日期:2014-05-17  浏览次数:20487 次

GridView RowCommand中的e.CommandArgument

C# code

前台:<asp:ButtonField CommandName="update" HeaderText="操作" Text="编辑" />
后台:
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gv.PageIndex = e.NewPageIndex;
        this.gv.DataBind();
        Bind();
    }
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument); 
        if (e.CommandName == "update")
        {
            int i = int.Parse(this.gv.DataKeys[index].Value.ToString());
            Label2.Text = i.ToString();//传递所点击绑定数据行的id值
            Label lab = (Label)(this.gv.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].FindControl("Label1"));
            string str = lab.Text;

            this.tTitle.Text = this.gv.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text.Trim();
            this.tContent.Text = str;
        }
}




点击下一页出现如图所示的错误。请教高手是哪里有问题,谢谢!

------解决方案--------------------
<asp:ButtonField CommandName="update" HeaderText="操作" Text="编辑" />
应该转成模板列再把数据绑定到CommandArgument='<%# Bind("你的值") %>'上
------解决方案--------------------
if (e.CommandName == "update")
{
int index = Convert.ToInt32(e.CommandArgument); int i = int.Parse(this.gv.DataKeys[index].Value.ToString());
Label2.Text = i.ToString();//传递所点击绑定数据行的id值
Label lab = (Label)(this.gv.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].FindControl("Label1"));
string str = lab.Text;

this.tTitle.Text = this.gv.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text.Trim();
this.tContent.Text = str;
}

------解决方案--------------------
<asp:ButtonField CommandName="update" CommandArgument='<%# Bind("你的值") %>' HeaderText="操作" Text="编辑" />

这样写!
------解决方案--------------------
探讨
C# code

前台:<asp:ButtonField CommandName="update" HeaderText="操作" Text="编辑" />
后台:
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.gv.PageIndex = e.NewP……

------解决方案--------------------
探讨
引用:
<asp:ButtonField CommandName="update" CommandArgument='<%# Bind("你的值") %>' HeaderText="操作" Text="编辑" />

这样写!


ButtonField中没有CommandArgument这个属性。

------解决方案--------------------
GridViewRow grow = (e.CommandSource as Button).Parent.Parent as GridViewRow;
Label lb= grow.Cells[要查找控件所在列表中的列的索引].FindControl("Label1") as Label;
//Label lb = gvs.Rows[grow.RowIndex].Cells[要查找控件所在列表中的列的索引].FindControl("Label1") as Label;