Determine Row Index - Template Control Buttons in Gridview

Posted on 2007-08-06
Last Modified: 2013-11-05
I have gridview control which had rows containing 2 button colums which was working ok. I was using this syntax to determine the row & what action to take in the code behind...

     Private Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim row As GridViewRow = Me.GridView1.Rows(index)
        Me.GridView1.SelectedIndex = index

        If e.CommandName = "Exclude" Then

However I have converted the buttons to template controls & now Im stumped because the RowCommand event doesnt fire anymore & I cant see how to determine the row index in the buttons click event

How do I get the row index for button clicked in a template control?
Question by:WNottsC
    LVL 16

    Expert Comment

    Make sure that your templated buttons have a CommandName attribute set which should force the RowCommand event to fire. To get the index of the row in which the clicked button was pressed, you need to set the CommandArgument attribute of your buttons in the RowDataBound event handler of your grid view.

    If your template column looks something like this:
        <asp:Button id="btnRowButton" runat="server" CommandName="Exclude" Text="Exclude" />

    Then in your RowDataBound Event handler do the following:
    If e.Row.RowType = DataControlRowType.DataRow Then
      Dim btnRowButton As Button = DirectCast(e.Row.FindControl("btnRowButton"), Button)
      btnRowButton.CommandArgument = e.Row.RowIndex
    End If
    LVL 96

    Expert Comment

    by:Bob Learned
    How are the controls defined in the TemplateField?  Did you define any event handlers in the HTML designer?

    LVL 8

    Expert Comment

    LinkButton button = (LinkButton)sender;
            GridViewRow grdRow = (GridViewRow)button.Parent.Parent;

    Try some thing like this

    Author Comment

    I havent added any code in html designer, do I need to?  I did 'convert to template' from with the grid designer then edited the template & added a button.

    The CommandName is set but RowCommand does not fire.

    Ive added the bit to save the rowindex in the commandargument & that seems to work ok but like I say the RowCommand is not firing so cant do much with it :(
    LVL 96

    Expert Comment

    by:Bob Learned
    If you have a TemplateField, then the controls are defined in the HTML designer.  That was what 'Convert to Template' did for you.

    LVL 16

    Accepted Solution

    What is your CommandName set to? There are a couple built in commandname's that ASP.NET uses which will not fire the standard RowCommand event.

    Author Comment


    Deleteing the buttons & recreting them fixed the event together with the RowDataBound event works ok now.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now