WNottsC
asked on
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. GridViewCo mmandEvent Args) Handles GridView1.RowCommand
Dim index As Integer = Convert.ToInt32(e.CommandA rgument)
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?
Private Sub GridView1_RowCommand(ByVal
Dim index As Integer = Convert.ToInt32(e.CommandA
Dim row As GridViewRow = Me.GridView1.Rows(index)
Me.GridView1.SelectedIndex
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?
How are the controls defined in the TemplateField? Did you define any event handlers in the HTML designer?
Bob
Bob
LinkButton button = (LinkButton)sender;
GridViewRow grdRow = (GridViewRow)button.Parent .Parent;
Try some thing like this
GridViewRow grdRow = (GridViewRow)button.Parent
Try some thing like this
ASKER
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 :(
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 :(
If you have a TemplateField, then the controls are defined in the HTML designer. That was what 'Convert to Template' did for you.
Bob
Bob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
Deleteing the buttons & recreting them fixed the event together with the RowDataBound event works ok now.
Deleteing the buttons & recreting them fixed the event together with the RowDataBound event works ok now.
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
Dim btnRowButton As Button = DirectCast(e.Row.FindContr
btnRowButton.CommandArgume
End If