• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1859
  • Last Modified:

Determine Row Index - Template Control Buttons in Gridview

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
               etc....

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?
0
WNottsC
Asked:
WNottsC
  • 2
  • 2
  • 2
  • +1
1 Solution
 
TSmoothCommented:
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:TemplateField>
  <ItemTemplate>
    <asp:Button id="btnRowButton" runat="server" CommandName="Exclude" Text="Exclude" />
  </ItemTemplate>
</asp:TemplateField>

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
0
 
Bob LearnedCommented:
How are the controls defined in the TemplateField?  Did you define any event handlers in the HTML designer?

Bob
0
 
sabeeshCommented:
LinkButton button = (LinkButton)sender;
        GridViewRow grdRow = (GridViewRow)button.Parent.Parent;

Try some thing like this
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
WNottsCAuthor Commented:
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 :(
0
 
Bob LearnedCommented:
If you have a TemplateField, then the controls are defined in the HTML designer.  That was what 'Convert to Template' did for you.

Bob
0
 
TSmoothCommented:
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.
0
 
WNottsCAuthor Commented:
Thanks

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now