I principal.aspx page, contains a GridView1 with fields Id and Name, Id is the DataKey.
with a selected row of gridview, and I click a button and open the page editar.aspx
Cut how to get the Id (datakey gridview) with a button
Cut how to get the Id (datakey gridview) with a button
protected void Button1_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
GridViewRow row = (GridViewRow)b.NamingContainer;
if (row != null)
{
//Get the Row Index
int rowIndex = row.RowIndex;
//Get the DataKey value
string key = GridView1.DataKeys[rowIndex].Value.ToString();
//Get the Row value of BoundField
string rowValue = row.Cell[0].Text;
}
}
alvaro_web
Member
121 Points
172 Posts
Get Datakey gridview
Jul 04, 2012 12:03 PM|LINK
Using VS2010, C #, AspNet4
I principal.aspx page, contains a GridView1 with fields Id and Name, Id is the DataKey.
with a selected row of gridview, and I click a button and open the page editar.aspx
Cut how to get the Id (datakey gridview) with a button
<asp:Button ID="BtnEditar" runat="server" Text="Editar" PostBackUrl="~/editar.aspx" />
shivalthakur
Participant
1837 Points
531 Posts
Re: Get Datakey gridview
Jul 04, 2012 12:07 PM|LINK
you can use query string
try this
<asp:Button ID="BtnEditar" runat="server" Text="Editar" PostBackUrl="~/editar.aspx?<%#Eval("id")%>" />
i am not sure but you can try this
hope this will help
Response.Write("Success");
Best Of Luck
Shival Thakur
alvaro_web
Member
121 Points
172 Posts
Re: Get Datakey gridview
Jul 04, 2012 12:22 PM|LINK
my gridview
<asp:GridView ID="GVSerie" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"/>
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name"/>
</Columns>
UstesG
Contributor
2098 Points
449 Posts
Re: Get Datakey gridview
Jul 04, 2012 12:59 PM|LINK
in the button click event:
gridview1.DataKeys(gridview1.SelectedIndex).Value
But don't expect me to do your job!
vinz
All-Star
126896 Points
17922 Posts
MVP
Re: Get Datakey gridview
Jul 05, 2012 12:14 PM|LINK
protected void Button1_Click(object sender, EventArgs e) { Button b = (Button)sender; GridViewRow row = (GridViewRow)b.NamingContainer; if (row != null) { //Get the Row Index int rowIndex = row.RowIndex; //Get the DataKey value string key = GridView1.DataKeys[rowIndex].Value.ToString(); //Get the Row value of BoundField string rowValue = row.Cell[0].Text; } }You may want to remove the PostBackUrl of the Button so that the code behind your Button will fire. You can use plain Response.Redirect instead.
MessageBox Controls for WebForms | Blog | Twitter | Linkedin