在RowCommand里怎么才能读到GridView的Value值!
我在操作当前行时同样到读取到当行里的DropDownList的Value值,还有当前行的主键值,
希望哪位仁兄帮我解决下这个问题呀,

解决方案 »

  1.   

    //当前行的主键值
       string id = this.GridView1.DataKeys[e.Row.RowIndex]["id"].ToString();
     //找到DropDownList
       DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("DropDownList1");
      

  2.   

    我在RowCommand事件里对当前行进行操作,
    但同时要在RowCommand事件如何才能读取到当行里的DropDownList的Value值,和当前行的主键值,
      

  3.   

    //当前行的主键值
       string id = this.GridView1.DataKeys[e.Row.RowIndex]["id"].ToString();
     //找到DropDownList
       DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("DropDownList1");
     但在RowCommand事件里并不包含对 e.Row.Cells 的定义
      

  4.   

    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);
            }
        }
    }