日期:2014-05-19  浏览次数:20836 次

gridview的问题,如何在rowcommand里获取DropDownList的值!
在RowCommand里怎么才能读到GridView的Value值!
我在操作当前行时同样到读取到当行里的DropDownList的Value值,还有当前行的主键值,


希望哪位仁兄帮我解决下这个问题呀,

------解决方案--------------------
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);
}
}
}