GridView的RowCommand事件中取得控件值
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);
}
}
}
所以你要先在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);
}
}
}