I have a GridView control and I addeda ‘ButtonField’ to it. I am trying to wire the ‘onClick’ event of the button to a procedure and pass the ID of the line item from which the button was clicked.
Convert your Button field to a Template field with a Button in it and use the GridView RowCreated or RowDataBound event to set the CommandArgument of the button to the RowIndex.
You can assign the id to the CommandArgument when grid is created or assign the rowindex and retrieve the DataKey on the click event of the button. Makes no difference but I prefer latter for a more consistent approach.
Tim
Marked as answer by samirayes on Sep 11, 2006 04:24 PM
Protected
Sub GridView1_RowCommand(ByVal sender
As Object,
ByVal e As GridViewCommandEventArgs)
Dim rIndex
As Integer =
CType(e.CommandArgument,
Integer)
Dim thisID As
String = Me.GridView1.DataKeys(rIndex).Value.ToString()
I like the idea of just accessing the key value of the row being clicked but could you expound more about doing so? I have a grid with a buttonfield. I can set the command name but not the argument. Then I assume the function that would be called when the
user clicks on the button is OnRowCommand? Then once in the code behind how do you know which button was selected if you don't have the argument already set?
If anyone could provide a simple example of gridview using a buttonfield that when clicked in the code behind i can get to the value of my row I'd appriecieate it!
grid viewButtonFieldcommand argument
~The KID
Don't forget to mark my response as ANSWERED if you found it helpful
samirayes
Member
221 Points
66 Posts
Pass the ID in a gridView ButtonField!!
Sep 07, 2006 03:13 PM|LINK
Hi
I have a GridView control and I added a ‘ButtonField’ to it. I am trying to wire the ‘onClick’ event of the button to a procedure and pass the ID of the line item from which the button was clicked.
Is there a simple to do this?
The GridView code:
<asp:GridView
DataSourceID="dataSrcID"
ID="gv_1" runat="server"
DataKeyNames="theID"
AutoGenerateColumns="false"
AutoGenerateDeleteButton="true"
AutoGenerateEditButton="true"
>
<Columns>
<asp:CheckBoxField DataField="field1" HeaderText="Field 1" ReadOnly="false" />
<asp:BoundField DataField="field2" HeaderText="Field 2" ReadOnly="false" />
<asp:BoundField DataField="Date" DataFormatString="{0:MM/dd/yyyy}" HeaderText="Date" HtmlEncode="false" ReadOnly="false" />
<asp:ButtonField ButtonType="link" Text="Find similar" /> 'this is where I want to pass the 'ID' to procedure
</Columns>
</asp:GridView>
I’ts got to be me – I don’t know what I’m missing, but I can’t seem to find a way to do it.
Help.
cashmore
Contributor
2941 Points
552 Posts
Re: Pass the ID in a gridView ButtonField!!
Sep 07, 2006 05:54 PM|LINK
Convert your Button field to a Template field with a Button in it and use the GridView RowCreated or RowDataBound event to set the CommandArgument of the button to the RowIndex.
Tim
samirayes
Member
221 Points
66 Posts
Re: Pass the ID in a gridView ButtonField!!
Sep 08, 2006 12:47 AM|LINK
I am looking for a way to pass the ID as a command arg - unless there is a way to use the index of the row clicked to get the ID.
I've been searching this all over and could not find an answer - is it a hidden art?
Is there a way to assign the ID to the command argument in a button field?
Thanks.
rexlin
Star
9403 Points
1751 Posts
Re: Pass the ID in a gridView ButtonField!!
Sep 08, 2006 01:24 AM|LINK
Hi,samirayes:
May this helps:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType ==DataControlRowType.DataRow)
{
//Button myButton = (Button)e.Row.Cells[1].Controls[0];
//myButton.CommandArgument = e.Row.RowIndex.ToString();
}
}
Best Regards,
__________________________________________________
Sincerely,
Rex Lin
Microsoft Online Community Support
This posting is provided "AS IS" with on warranties, and confers no rights.
cashmore
Contributor
2941 Points
552 Posts
Re: Pass the ID in a gridView ButtonField!!
Sep 08, 2006 01:38 AM|LINK
For the Id use - gv_1.DataKeys(rowindex).Value
You can assign the id to the CommandArgument when grid is created or assign the rowindex and retrieve the DataKey on the click event of the button. Makes no difference but I prefer latter for a more consistent approach.
Tim
samirayes
Member
221 Points
66 Posts
Re: Pass the ID in a gridView ButtonField!!
Sep 11, 2006 04:28 PM|LINK
Thank you - I get it now...
The solution:
Protected Sub gv_rowCommand(s As Object, e As GridViewCommandEventArgs)
Dim rIndex As integer = CType(e.CommandArgument, Integer)
Dim thisID As String = Me.gv_listTitles.DataKeys(rIndex).Value.ToString()
End Sub
JLeo1977
Member
262 Points
79 Posts
Re: Pass the ID in a gridView ButtonField!!
May 01, 2008 09:21 PM|LINK
Don't Forget that you have to wire up the aspx page's formview control using the OnRowCommand construction:
ASPX Page:
<
asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" OnRowCommand="GridView1_RowCommand" DataSourceID="SqlDataSource1" Width="99%">ASPX.VB Page:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Dim rIndex As Integer = CType(e.CommandArgument, Integer) Dim thisID As String = Me.GridView1.DataKeys(rIndex).Value.ToString()Response.Write(thisID)
End Subicereval
Member
8 Points
4 Posts
Re: Pass the ID in a gridView ButtonField!!
Nov 27, 2008 01:51 AM|LINK
FYI, remember to set the DataKeyNames property when you define your GridView if you want to use the DataKeys example above.
The_ASP_KID
Member
30 Points
36 Posts
Re: Pass the ID in a gridView ButtonField!!
Apr 20, 2009 10:42 PM|LINK
I like the idea of just accessing the key value of the row being clicked but could you expound more about doing so? I have a grid with a buttonfield. I can set the command name but not the argument. Then I assume the function that would be called when the user clicks on the button is OnRowCommand? Then once in the code behind how do you know which button was selected if you don't have the argument already set?
If anyone could provide a simple example of gridview using a buttonfield that when clicked in the code behind i can get to the value of my row I'd appriecieate it!
grid view ButtonField command argument
Don't forget to mark my response as ANSWERED if you found it helpful
NicoVenema
Member
20 Points
5 Posts
Re: Pass the ID in a gridView ButtonField!!
Feb 11, 2010 07:51 AM|LINK
I would prefer
<asp:TemplateField HeaderText="Remove" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="btnRemoveItem" runat="server" CausesValidation="false" Text="remove" CommandArgument='<%# eval("MyRowId") %>' oncommand="btnRemove_Command" />
</ItemTemplate>
</asp:TemplateField>
With code:
Protected Sub btnRemove_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
Dim myId As Integer = e.CommandArgument
End Sub
databinding gridView buttonfield linkbutton