您的位置:首页 > 编程语言 > ASP

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

2013-09-25 22:15 531 查看
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 属性可确定正在创建的是哪一种行类型(标题行、数据行等等)。

以此获得行参数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  asp.net gridview 控件