这样写protected void LinkButton1_Click(object sender, EventArgs e) { GridViewRow r = (GridViewRow)GridView1.Rows; int i = Convert.ToInt32(GridView1.DataKeys[r.RowIndex].Value);//i就是要获取的值 }
protected void Linkbutton1_SelectedIndexChanged(object sender, EventArgs e) { Linkbutton DropDownList1 = sender as Linkbutton; int index = (DropDownList1.NamingContainer as GridViewRow).RowIndex; string id = GridView1.DataKeys[index].Value.ToString(); }
<asp:LinkButton ID="lbtn" runat="server" OnClick="Linkbutton1_Click"/> protected void Linkbutton1_SelectedIndexChanged(object sender, EventArgs e) { Linkbutton Linkbutton1 = sender as Linkbutton; int index = ( Linkbutton1.NamingContainer as GridViewRow).RowIndex; string id = GridView1.DataKeys[index].Value.ToString(); }
后台代码是这样:
protected void LinkButton1_Click(object sender, EventArgs e)
{
int row = ((GridViewRow)((LinkButton)sender).NamingContainer).DataItemIndex;
myTool.Alert(row.ToString(), this);
}
现在这个得到的是当前行
但是我需要得到的是DataKeyNames的值,也就是数据库中的id值,而不是当前行。
数据库中的id值可能是不连续的,而当前行一定是连续的。
{
GridViewRow r = (GridViewRow)GridView1.Rows;
int i = Convert.ToInt32(GridView1.DataKeys[r.RowIndex].Value);//i就是要获取的值
}
{
Linkbutton DropDownList1 = sender as Linkbutton;
int index = (DropDownList1.NamingContainer as GridViewRow).RowIndex;
string id = GridView1.DataKeys[index].Value.ToString();
}
<asp:LinkButton ID="lbtn" runat="server" OnClick="Linkbutton1_Click"/>
protected void Linkbutton1_SelectedIndexChanged(object sender, EventArgs e)
{
Linkbutton Linkbutton1 = sender as Linkbutton;
int index = ( Linkbutton1.NamingContainer as GridViewRow).RowIndex;
string id = GridView1.DataKeys[index].Value.ToString();
}
如果选MVP了,告诉我一声,我绝对投你的票~~
在GridView的SelectedIndexChanged事件中,一句代码就可获得!! protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string a = GridView1.SelectedDataKey.Value.ToString();
}