[RESOLVED] [2005] CommandArgument Gridview Itemtemplate
Hey,
I know I've seen this around before but I forgot how to do it... I have a couple buttons within a Gridview's Itemtemplate. I need to assign the commandargument to the buttons and not sure exactly how to do it...
I've been messing around in the RowCreated event and just in the markup
Code:
<asp:Button ID="btnDetails" runat="server" CommandName="Details" CommandArgument='<%# notsurewhattoputhere %>' Text="Details" Width="80px" /><br />
Code:
Protected Sub gvItemGrid_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvItemGrid.RowCreated
If e.Row.RowIndex > 0 Then
Dim btnDetails As Button = DirectCast(e.Row.Cells(5).FindControl("btnDetails"), Button)
btnDetails.CommandArgument = e.Row.RowIndex.ToString
End If
End Sub
Re: [2005] CommandArgument Gridview Itemtemplate
You want the RowDataBound event.
Do the FindControl (e.Row.FindControl()), cast it to a button, then assign the CommandArgument and CommandName properties.
Re: [2005] CommandArgument Gridview Itemtemplate
Cool thanks... Can it be done in the Markup also? or should I just stick with the databound event?
Re: [2005] CommandArgument Gridview Itemtemplate
You can do it in markup as well. I would suggest setting the CommandName in markup (if it's not a different name for different rows) but the command argument you should do in the event mentioned before by mendhak. In the markup it would be something like
Code:
<asp:Button ID="editButton" runat="server" CommandName="EditContainer" Text="Edit"
CommandArgument="<%# CType(Container, GridViewRow).RowIndex %>" />
But as you can see the code to set the command argument is .Net code and is not suggested to be put in markup.