现在的位置: 首页 > 综合 > 正文

GridView的RowCommand事件中取得控件值

2012年09月04日 ⁄ 综合 ⁄ 共 889字 ⁄ 字号 评论关闭
GridViewCommandEventArgs 类未包含一个用于指示单击按钮所在行的属性。如果需要知道哪个行引发了事件,请使用 CommandArgument 属性将行的索引传给事件处理方法。

所以你要先在RowCreated(其中Test是按钮的CommandName)
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    LinkButton addButton ;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        addButton = (LinkButton)e.Row.Cells[1].Controls[0];
        if (addButton != null)
        {
            if (addButton.CommandName== "Test")
                addButton.CommandArgument = e.Row.RowIndex.ToString();
        }
    }
}

然后在RowCommand事件中
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{        
    if (e.CommandName == "Test")
    {
        DropDownList dropTemp = (DropDownList)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].FindControl("dropTemp");
        if (dropTemp != null)
        {
            Response.Write(dropTemp.Text);
        }
    }
}

抱歉!评论已关闭.