ButtonField to TemplateField with Button

E

Elmo Watson

I've converted a buttonfield to a TemplateField with a button in it

Previously, in the Rowcommand, to identify the row when the ButtonField was
clicked, I had this code:

Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = MyGrid.Rows(index)

However, with the new TemplateField (with a button in the TemplateField), in
the RowCommand, it does not understand 'e.CommandArgument'

I get the error that the "input string was not in the correct format"

What am i missing here?
 
M

Milosz Skalecki [MCAD]

Hi Elmo,

ButtonField automatically puts the index to CommandArgument property of the
button. Now, because you changed it to TemplateField you are responsible for
binding the value manually yourself:

<asp:GridView runat="server" ID="MyGrid" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="btn"
CommandName="MyCustomCommand"
CommandArgument='<%# Container.DataItemIndex
%>' Text="Click Me" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

And the vb.net code behind:

Protected Sub MyGrid_RowCommand(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) _
Handles MyGrid.RowCommand

If e.CommandName = "MyCustomCommand" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = MyGrid.Rows(index)
End If

End Sub

Hope it helps
 
D

dbelogrlic

Hi Elmo,

ButtonField automatically puts the index to CommandArgument property of the
button. Now, because you changed it to TemplateField you are responsible for
binding the value manually yourself:

<asp:GridView runat="server" ID="MyGrid" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="btn"
CommandName="MyCustomCommand"
CommandArgument='<%# Container.DataItemIndex
%>' Text="Click Me" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

And the vb.net code behind:

Protected Sub MyGrid_RowCommand(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) _
Handles MyGrid.RowCommand

If e.CommandName = "MyCustomCommand" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = MyGrid.Rows(index)
End If

End Sub

Hope it helps

Thank you,Milos ....You are MASTER
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top