Click here to Skip to main content
15,121,414 members
Articles / Web Development / ASP.NET
Posted 30 Oct 2007


52 bookmarked

GridView Hover Menu

Rate me:
Please Sign up or sign in to vote.
3.17/5 (13 votes)
30 Oct 20076 min read
An article on displaying row-databound command fields in a girdview as a popup menu within a div
Screenshot - gridview1.jpg




So, what are we talking about here? Let's say we have a gridview and have some command fields inside the grid, like Edit, Delete, NavigatetoURL and so on. It'll look clumsy to have all these commands (linkbuttons or hyperlinks) in seperate columns or even in one column.


Sometimes, we might want some commands to show up based on the row's data. In the page that I was working on, I have to show PRINT, EMAIL, EDIT and PREVIEW buttons for each row in a gridview. Moreover, not all rows have the same buttons. Some rows cannot be editied and some rows cannot be emailed (when there is no email address to email).


If you all have checked AJAX control toolkit, the same is done in a easy and elegant way. Click here to see AJAX hover menu in action. Scenario 2 is what makes our code different.

Using the Code (Read Before Continuing)

Assuming that you know about gridview and the events associated with gridview, I am not going in depth on how to implement this mouseover pop-up menu. Also, I assume that you're familiar with HTML elements like DIV, and positioning of these elements. The JavaScript support methods that I have used are very simple and easy to use and are scalable.


So, what can we do to have the two functionalities discussed in the introduction of this article and at the same time make our gridview look pretty? Well here we are:

Screenshot - gridview2.jpg

The above is a regular gridview that we all might have seen and implemented. It's plain and doesn't implement the command fields as of now. As you know we usually have the last or first column containing command fields like EDIT (UPDATE & CANCEL), DELETE and so on.

To make this gridview do what looks like the first image at the top of this article, let's start with creating the popup menu first. Before creating the popup menu, think of where you're going to place it. By that I mean where do you want it to popup. I usually prefer that the popup shows up at the last column and just a little inside to the grid (from the first figure) so that it overlaps the gridview but, doesn't block the contents of the last column.

<asp: templatefield headertext="Status"
<asp:label id="lblStatus" Text='<%# Bind("Status")%>'/>
<div id="gridPopup" style="DISPLAY:none; Z-INDEX:101; POSITION: absolute; 
   HEIGHT:38px" runat="server">
<asp:linkbutton id="lbtngrdPrint" text="PRINT" runat="server" 
    commandargument='<%#Bind("Status")%>' commandname="Print" 
<asp:linkbutton id="lbtngrdEmail" text="SEND" runat="server" 
    commandargument='<%#Bind("Status")%>' commandname="Email" 

Now this is the template for the last column. That is where I am going to add my DIV popup. As you can see, I currently have two link buttons inside the DIV. You can have any number of buttons you want. Also, you can apply styles to it to make it look pretty. I leave that portion to you.

As you can see, I am placing the DIV tag with all the applicable menu items (as linkbuttons) within the last column's itemtemplate section. Note that I also have set the z-index of the DIV tag and an absolute positioning. This is vital for the positioning of the DIV. Since I have the DIV inside the itemtemplate, I can bind it with a dataitem and even give it a command name.

Now that we have the markup in place, let's write some client-side code to toggle the DIV's visibility ON and OFF.

// Shows DIV popup commands for gridview
function ShowPopup(lbtn1,lbtn2, panel, gridviewRow)
    var link1 = document.getElementById(lbtn1);
    var link2 = document.getElementById(lbtn2);
    var pnl = document.getElementById(panel);
    var row= document.getElementById(gridviewRow); = "block"; = "url(../images/td_mouseover.gif)";
    if(link1 != null) = "block";
    if(link2 != null) = "block"; = "url(../images/td_mouseover_inverted.gif)";

//Hides DIV popup commands for gridview
function HidePopup(lbtn1,lbtn2, panel, gridviewRow)
    var link1 = document.getElementById(lbtn1);
    var link2 = document.getElementById(lbtn2);

    var pnl = document.getElementById(panel);
    var row= document.getElementById(gridviewRow);"url(../images/spacer.gif)"; = "none";
    if(link1 != null) = "none";
    if(link2 != null) = "none";

Understanding this script is very simple. The parameters you saw in the two functions ShowPopup and HidePopup are described below:

  • lbtn1: LinkButton1. I just named it this way so that I'll know that it is the first button in our menu. Remember to databind it because you're putting it inside a gridview.
  • lbtn2: LinkButton2. The second link button in the DIV popup. As I mentioned earlier, you can have any number of controls (usually buttons or hyperlinks for our purposes) inside the DIV. All you then have to do is add the id of that control as a parameter in your JavaScript functions and set the visibility to 'none' or 'block'.
  • panel: I just call the DIV layer as panel because, essentially they're the same. You have to toggle visibility for the DIV (or the panel as I call it here) on and off first before you toggle the visibility of the menu items inside it (the linkbuttons and/or any other controls that you might have).

Of course the last parameter is the gridview row which is taken as <TD> when the page is rendered.

Customizing the Menu Items Inside the DIV Popup

If you see the image of the new gridview, it will show you options to PRINT and EMAIL. You can set the visibility of these items ON or OFF on the row's mouseover event. That you already know. Now, the interesting part is, you can display row-specfic command fields based on a value from the row. That means, you can display only the PRINT button for certain rows, and EMAIL button for certain rows. Of course you can have permutations and combinations for this. How do we go about doing that?

Well, here is the Row Data Bound event for the gridview. As we know, this event fires up when a gridview row is data-bound. So, this is probably the best place to set row specific scripts and conditions.

protected void
gvwMyReferrals_RowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.DataRow)
        LinkButton lbtnGrdPrint = (LinkButton)e.Row.Cells[6].FindControl(
        LinkButton lbtnGrdEmail = (LinkButton)e.Row.Cells[6].FindControl(
        HtmlGenericControl panel = 

        string showPopup = "ShowPopup('" + lbtnGrdPrint.ClientID + 
            "','" + lbtnGrdEmail.ClientID + "','" + 
            panel.ClientID + "','"+e.Row.ClientID+"')";
        string hidePopup = "HidePopup('" + lbtnGrdPrint.ClientID + 
            "','" + lbtnGrdEmail.ClientID
            + "','" + panel.ClientID + "','" + 
            e.Row.ClientID + "')";
            "javascript:" +hidePopup);

I first check to see if the row is a data row (which eliminates pager, header and footer rows). Then, I get the instance of the controls that make up the DIV popup menus. First I get the instances of the linkbuttons and then I get the instance of the DIV tag. Notice that I am casting the DIV as a panel? Well, that is not casting! Since the panel is a DIV tag, this method works fine and does not throw casting exceptions. If you have more controls inside the DIV, get instances of those controls and also the instance of the DIV.

Now that we have instances of the neccasary contols, it is time to call the client script functions but, not just yet. I'll explain what I have not implemented as of yet.

Remember me writing that you can have row-specific menu items inside the DIV? Let's discuss that a little. In the row data bound, we can get some value from the data source that is bound to a boundfield or template field, and based on that, we can manipulate the visibility of the controls inside the link button. In my example (see the image of the new gridview), I have set the DIV tag to be a part of the last column with name as STATUS. Now, I can use STATUS's cell data to display the linkbuttons. All I have to do is, within an if statement, check the STATUS's string value and based on some condition, I can either pass that linkbutton's client ID to the script or, I can pass a null value instead of the client ID if I don't want that linkbutton to show up. When I pass the null value to the script, it ignores that parameter based on this condition:

if(link1 != null) = "none";

This will make sure the script doesn't blow up. Now, when you hover over each row, you can have menu specific items inside your DIV popup. Since I am also passing e.Row to the script, I can make the whole row's style change on an onmouseover event. That will give the grid a look and feel of a clickable row. Of course you can extend it by adding styles through JavaScript.

Final Touchup

I have set the row background and the popup DIV's background using JavaScript. The GIF file td_mouseover.gif is just a gradient image that gives a emboss effect on the row when you an onmouseover event occurs. Likewise, the file td_mouseover_inverted.gif is a gradient background for the DIV.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

marshal v stephen
Web Developer
No Biography provided

Comments and Discussions

General[My vote of 1] My vote of 1 Pin
Bonus Kun15-Oct-15 0:13
MemberBonus Kun15-Oct-15 0:13 
Questionthank you so much Pin
Awaidus Bin Asim7-Sep-14 9:19
MemberAwaidus Bin Asim7-Sep-14 9:19 
GeneralMy vote of 5 Pin
Ajith_joseph13-Aug-12 5:18
MemberAjith_joseph13-Aug-12 5:18 
GeneralGood Article Pin
Sujeet Bhujbal24-Oct-11 21:19
MemberSujeet Bhujbal24-Oct-11 21:19 
QuestionSample Source Code needed!!!!! Pin
Oluleke6-Jul-11 8:11
MemberOluleke6-Jul-11 8:11 
GeneralRight click event Pin
enkaysmitha7-Oct-10 22:31
Memberenkaysmitha7-Oct-10 22:31 
GeneralMy vote of 1 Pin
veeraboyina.vamsikrishna7-Sep-10 22:22
Memberveeraboyina.vamsikrishna7-Sep-10 22:22 
Generalerrors in code, and where is the source Pin
SQueeZe76519-May-09 20:02
MemberSQueeZe76519-May-09 20:02 
Generaldifficulty in Gridview Popup Selection Pin
drvuk13-Oct-08 3:09
Memberdrvuk13-Oct-08 3:09 
QuestionGridView Hover Menu Pin
Member 304839326-Jun-08 11:22
MemberMember 304839326-Jun-08 11:22 
GeneralCode download Pin
LotusShiv5-Feb-08 3:34
professionalLotusShiv5-Feb-08 3:34 
GeneralRe: Code download Pin
marshal v stephen5-Feb-08 13:43
Membermarshal v stephen5-Feb-08 13:43 
GeneralLittle error Pin
DamianDS30-Oct-07 17:15
MemberDamianDS30-Oct-07 17:15 
marshal v stephen30-Oct-07 17:50
Membermarshal v stephen30-Oct-07 17:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.