wordpress批量拿站,凡科商城app下载,泉州网站建设服务,win系统做网站获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton前台 (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设置CommandArgument的值#xff0c;值为绑定的数据库字段asp:TemplateField HeaderText操作 …获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton前台 (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设置CommandArgument的值值为绑定的数据库字段asp:TemplateField HeaderText操作 ItemTemplate asp:LinkButton IDLinkButton1 runatserver CommandNameQianRu CommandArgument%# Eval(Id) %签入/asp:LinkButton asp:LinkButton IDLinkButton2 runatserver CommandNameQianChu签出/asp:LinkButton /ItemTemplate/asp:TemplateField 后台在GridView里已经设置了LinkButton为事件处理按钮将通过以下方法获取索引protected void gv_Company_RowCommand(object sender, GridViewCommandEventArgs e){ if (e.CommandName QianRu) { //取ID的值方法一 GridViewRow drv ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 inf idConvert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value); //此获取的值为GridView中绑定数据库中的主键值 //取ID的值方法二 GridViewRow drv ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 //此获取的值为GridView中绑定数据库中的主键值,取值方法是选中的行中的第一列的值,drv.RowIndex取得是选中行的索引 int id Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text); //取ID的值方法三 //因为在客户端中就已经将LinkButton的CommandArgument与主键Id给绑定了所以在此可以直接用e.CommandArgument得出主键ID的值 int id Convert.ToInt32(e.CommandArgument.ToString()); //取ID的值方法四 //此方法不需在模板列中设置CommandArgument的值 string indexe.CommandArgument.ToString(); //那行被选中,取出选中行的索引 int idConvert.ToInt32(GridView1.Rows[Convert.ToInt32(index)].Cells[0].Text); } } 还有一种就是我们并不需要知道当前点击的是第几行可以用以下方法实现要求 ItemTemplate asp:LinkButton IDLinkButton1 runatserver CommandArgument %# Eval(field1) % CommandNameplay Text %# Eval(field2) % /asp:LinkButton /ItemTemplate 上面这个LinkButtonText绑定了字段2 CommandArgument绑定了字段1 那么 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if(e.CommandNameplay) { LinkButton lb (LinkButton)e.CommandSource; string a lb.Text;//这里可以获得点击行字段field2的值 string b e.CommandArgument;//这里可以获得点击行字段field1的值 }} 或如果是使用模板列可以把数据的任意一列绑定到按钮的CommandArgument如下 asp:TemplateField ItemTemplate asp:Button runatserver CommandArgument%# Eval(id) % TextButton / /ItemTemplate /asp:TemplateField 一般可以绑定到主键列这样可以在RowCommand通过e.CommandArgument获取当前行的主键也便于进行其他操作 如果是要获取行索引比较麻烦一点还是那个Button1在GridView的RowDataBound事件中如下 Button btn (Button)e.Row.FindControl(Button1); if (btn ! null) { btn.CommandArgument e.Row.RowIndex.ToString(); } 这样就可以在RowCommand中通过 int rowIdConvert.ToInt32(e.CommandArgument.ToString()) 获取行索引了 转载于:https://www.cnblogs.com/juan/archive/2009/03/31/1425928.html