日期:2014-05-18  浏览次数:20428 次

100分 :小问题。
gridview中我   最后一列为模板作了一个按钮。那么当我按此按钮时候,我如何能获取该按钮所在行的数据中的ID   ??
谢谢。

------解决方案--------------------
关注。。。帮顶。。。
------解决方案--------------------
在RowCommand 事件中

GridViewRow row = ((Control)e.CommandSource).BindingContainer as GridViewRow;
int index = row.RowIndex;

int strid = Convert.ToInt32(e.CommandArgument.ToString());
------解决方案--------------------
呵呵。不知道“所在行的数据中的ID”是什么结构,不方便乱说。
------解决方案--------------------
不明白还没有用过呢,学习一下
------解决方案--------------------
1. 给按钮个CommandName 假设为Test
2. 在GridView1_RowCreated,把当前行索引赋给按钮的CommandArgument属性

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
LinkButton addButton ;
if (e.Row.RowType == DataControlRowType.DataRow)
{
addButton = (LinkButton)e.Row.Cells[1].Controls[0];
if (addButton != null)
{
if (addButton.CommandName== "Test ")
addButton.CommandArgument = e.Row.RowIndex.ToString();
}
}
}
3. 在GridView1_RowCommand中进行处理...
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Test ")
{
Response.Write(((Label)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Controls[1]).Text);
}
}
------解决方案--------------------
绑定按钮的CommandArgument属性为ID
如:CommandArgument= ' <%# Eval( "id ") %> '
创建按钮的Command事件 Button1_Command
后台:
protected void Button1_Command(object sender, CommandEventArgs e)
{
int i=int.Parse(e.CommandArgument.tostring())
}
i就是你要获取的ID