5,696,576 members and growing! (12,898 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » Grid Controls     Intermediate License: The Code Project Open License (CPOL)

Hotmail-like Mouse Over & Mouse Out Effects on GridView

By Sameer NIGAM

This article describes how to apply client-side mouse over & mouse out effects on the GridView’s rows.
Javascript, CSS, HTML, .NET (.NET, .NET 2.0), ASP.NET, Dev

Posted: 30 Jan 2008
Updated: 8 Mar 2008
Views: 30,743
Bookmarked: 54 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
20 votes for this Article.
Popularity: 5.99 Rating: 4.61 out of 5
0 votes, 0.0%
1
1 vote, 5.0%
2
0 votes, 0.0%
3
1 vote, 5.0%
4
18 votes, 90.0%
5

Introduction

If you have a Hotmail Windows Live ID email account, then you have already observed mouse over and mouse out effects on the rows of an Inbox’s grid. On a mouse over event of any row of the Inbox’s grid, a CheckBox appears in place of an Image. Similarly, on a mouse out event, an Image appears in place of a CheckBox.

There are three types of images that appear in place of a CheckBox. If the message is unread, then the message.png Image appears. If the message is read, then the message_open.png Image appears. If the message has been replied to, then the message_reply.png Image appears.

Now if we make a CheckBox checked that appears on a row due to a mouse over event, then the mouse over and mouse out effects on that row get disabled. If we make that CheckBox unchecked, then the mouse over and mouse out effects start to work for that row again.

Now I'm going to present this functionality with the help of the GridView control. In this small application, I'm going to use only a single Image rather than use all three images. On a mouse over event for a row, a CheckBox will appear. On a mouse out event, an Image will appear. If we make CheckBox checked, then the mouse over and mouse out effects on that row will get disabled. Further, on making that CheckBox unchecked, mouse over and mouse out effects will start to work for that row again.

I have also implemented row coloring of GridView on mouse over and mouse out events in this application.

HTML Code

Use a TemplateField inside the GridView and put a CheckBox and an Image in the ItemTemplate of the TemplateField. The HTML code for GridView will look like this:

<asp:GridView ID="gvHotmail" runat="server" AutoGenerateColumns="False"
    OnRowDataBound="gvHotmail_RowDataBound">
            <Columns>
                <asp:BoundField HeaderText="n" DataField="n">
                    <HeaderStyle Width="50px" />
                    <ItemStyle Width="50px" />
                </asp:BoundField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <input id="chkBxMail" type="checkbox" runat="server" />
                        <asp:Image ID="imgMail" runat="server" ImageUrl="~/mail.png" />
                    </ItemTemplate>
                    <HeaderStyle Width="50px" />
                    <ItemStyle Width="50px" />
                </asp:TemplateField>
                <asp:BoundField HeaderText="n*n" DataField="nn">
                    <HeaderStyle Width="100px" />
                    <ItemStyle Width="100px" />
                </asp:BoundField>
            </Columns>
            <HeaderStyle Height="25px" BackColor="DarkOrange"
                HorizontalAlign="Center" VerticalAlign="Middle" />
            <RowStyle Height="25px" BackColor="NavajoWhite"
                HorizontalAlign="Center" VerticalAlign="Middle" />
            <AlternatingRowStyle Height="25px" BackColor="Gold"
                HorizontalAlign="Center" VerticalAlign="Middle" />
 </asp:GridView>

C# Code

Put the following code in the RowDataBound event of GridView:

if (e.Row.RowType == DataControlRowType.DataRow && (
    e.Row.RowState == DataControlRowState.Normal || e.Row.RowState ==
    DataControlRowState.Alternate))
        {
            HtmlInputCheckBox chkBxMail =
                (HtmlInputCheckBox)e.Row.Cells[1].FindControl("chkBxMail");
            Image imgMail = (Image)e.Row.Cells[1].FindControl("imgMail");

            e.Row.Attributes["onmouseover"] =
                string.Format("javascript:ItemMouseOver(this,'{0}','{1}');",
                chkBxMail.ClientID, imgMail.ClientID);
            e.Row.Attributes["onmouseout"] =
                string.Format("javascript:ItemMouseOut(this,'{0}','{1}');",
                chkBxMail.ClientID, imgMail.ClientID);

            chkBxMail.Attributes["style"] = "display:none;";
        } 

In the above code, first I have ensured that the row is either Normal or Alternate. After that, I have gotten the references of the CheckBox and Image controls and attached client-side mouse over and mouse out events on them. I have also added a style [display:none;] on the CheckBox control so that initially CheckBox will not appear.

JavaScript Code

Put this JavaScript in the page's head section.

<script type="text/javascript">
    function ItemMouseOver(oRow, chkBxMail, imgMail)
    {
        oCheckBox = document.getElementById(chkBxMail);
        oImage = document.getElementById(imgMail);

        if(!oCheckBox.checked)
        {
            oCheckBox.style.display = '';
            oImage.style.display = 'none';
            oRow.originalBackgroundColor = oRow.style.backgroundColor
            oRow.style.backgroundColor = 'White';
        }
    }

    function ItemMouseOut(oRow, chkBxMail, imgMail)
    {
        oCheckBox = document.getElementById(chkBxMail);
        oImage = document.getElementById(imgMail);

        if(!oCheckBox.checked)
        {
            oCheckBox.style.display = 'none';
            oImage.style.display = '';
            oRow.style.backgroundColor = oRow.originalBackgroundColor;
        }
    }
</script>

In the above script, there are two functions: ItemMouseOver and ItemMouseOut. ItemMouseOver is called on mouse over events and ItemMouseOut is called on mouse out events for GridView rows. In the ItemMouseOver function, CheckBox appears and Image disappears. In the ItemMouseOut function, CheckBox disappears and Image appears. In both functions, appearing/disappearing effects will reflect provided the CheckBox is unchecked. I have also used some coloring effects on mouse over and mouse out effects respectively.

Supporting Browsers

I have tested this script on the following browsers:

Browsers.png

History

  • 30 January, 2008 -- Original version posted
  • 6 February, 2008 -- Article edited
  • 5 March, 2008 -- Article updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sameer NIGAM


Currently I'm working as a Sr. Software Engineer in SRM Techsol Pvt. Ltd., Lucknow, INDIA. I possess following degrees:

  • MCA from U.P. Technical University, Lucknow, INDIA.
  • PGDCA from Institute of Engineering and Rural Technology, Allahabad, INDIA.
  • BSc from University of Allahabad, Allahabad, INDIA.


Award: Best ASP.NET article of June 2008: JavaScript ListBox Control

Occupation: Software Developer (Senior)
Company: STPL
Location: India India

Other popular ASP.NET Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
GeneralUsing CssmemberAbishek Bellamkonda14:16 4 Mar '08  
GeneralRe: Using Cssmember SAMir Nigam 19:14 21 May '08  
Questionproblem with ClientIDmembert-fuse22:33 28 Feb '08  
GeneralRe: problem with ClientIDmember Samir Nigam 17:58 3 Mar '08  
GeneralRe: problem with ClientIDmembert-fuse2:03 4 Mar '08  
GeneralRe: problem with ClientIDmember Samir Nigam 4:43 4 Mar '08  
GeneralNice effort keep it up.memberSecrets19:17 5 Feb '08  
GeneralRe: Nice effort keep it up.member Samir Nigam 21:39 5 Feb '08  
GeneralThe article is not matching at all as per one of our CriticizermemberSandeep Shekhar19:35 30 Jan '08  
GeneralRe: The article is not matching at all as per one of our CriticizermemberSamir Nigam19:46 30 Jan '08  
GeneralI did something simliarmvpSacha Barber5:40 30 Jan '08  
GeneralRe: I did something simliarmemberSamir Nigam19:04 30 Jan '08  
GeneralRe: I did something simliarmvpSacha Barber23:20 30 Jan '08  
GeneralRe: I did something simliar [modified]memberI Never Look Behind19:53 30 Jan '08  
GeneralRe: I did something simliarmvpSacha Barber23:20 30 Jan '08  
GeneralRe: I did something simliar [modified]memberRanjan.D20:03 9 Mar '08  
GeneralNicememberHonest2:20 30 Jan '08  
GeneralRe: NicememberI Never Look Behind19:54 30 Jan '08  
GeneralRe: NicememberSamir Nigam19:56 30 Jan '08  
GeneralHi SamirmemberMCSDvikasmisra2:04 30 Jan '08  
GeneralRe: Hi SamirmemberSamir Nigam2:12 30 Jan '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Mar 2008
Editor: Sean Ewington
Copyright 2008 by Sameer NIGAM
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project