如何在GridView的OnRowCommand事件 用FindControl找到模版控件?
Html:
CS:protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName.ToString().Trim() == "Edit")
{
string ClDId = ((Label)(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("Lab_DId"))).Text.Trim();//e.CommandArgument调试数据为空
string ClDId1 = ((Label)(GridView1.SelectedRow.FindControl("Lab_DId"))).Text.Trim();}
}
RowDataBound事件里面把行的索引绑定到button的CommandArgument 属性上面!
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton rb = (LinkButton)e.Row.FindControl("LinkButton1");
if (rb != null)
rb.CommandArgument = e.Row.RowIndex.ToString(); //把选中行的RowIndex也传过去,提交后在服务器端取值时用
}
}
然后rowcommand事件里面这么写就找到啦!
Label lab1 = (Label)(GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("Label1"));