asp.net gridview itemtemplate中控件事件获取行参数

gridview中加入了一列linkbutton,但是在rowcommand事件中,不能读取出来其中的行(e.commandAgurments),通过查阅资料,发现可以这么解决。
protected void gv_city_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "shenpi")//AAAA
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gv_city.Rows[index];
                Session["user_numB"] = row.Cells[1].Text.ToString();
                Response.Write("<script> alert('" + Session["user_numB"] + "'); </script>");
                //Response.Redirect("/login.aspx");
            }
        }
        protected void gv_city_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
            //Retrieve the LinkButton control from the first column.
                LinkButton lb_shenpi = (LinkButton)e.Row.FindControl("lb_shenpi");
            // Set the LinkButton's CommandArgument property with the row's index.
                lb_shenpi.CommandArgument = e.Row.RowIndex.ToString();                
            }
        }

msdn中的解释是

呈现 GridView 控件之前,必须先为该控件中的每一行创建一个 GridViewRow 对象。 在创建 GridView 控件中的每一行时,将引发 RowCreated 事件。 这使您可以提供一个这样的事件处理方法,即每次发生此事件时就执行一个自定义例程(如在行中添加自定义内容)。
GridViewRowEventArgs 对象将传递给事件处理方法,以便您可以访问正在创建的行的属性。 若要访问行中的特定单元格,请使用 GridViewRowEventArgs 对象的 Cells 属性。 使用 RowType 属性可确定正在创建的是哪一种行类型(标题行、数据行等等)。

以此获得行参数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值