<asp:TemplateField HeaderText="机型">
                    <ItemTemplate>
                    <asp:DropDownList runat="server" ID="drop1"  DataSourceID="ObjectDataSource2"  DataTextField="M_type"  DataValueField="id" SelectedValue='<%# Bind("m_id") %>' Enabled="false" ></asp:DropDownList>                    
                    </ItemTemplate>
                    </asp:TemplateField>
在asp.net 中 怎么才能取到asp:GridView 控件中asp:DropDownList 的值?

解决方案 »

  1.   

    有个FindControl函数,仔细看看他的用法.只需要找到ID="drop1"就行了
      

  2.   

    DropDownList drop = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drop1");
    string zhi = drop.selectValue;
      

  3.   

    ((GridView1.Rows[索引].FindControl("drop1")) as DropDownList).SelectedValue;
      

  4.   

    在asp.net 中 怎么才能取到asp:GridView 控件中asp:DropDownList 的值?
    ==
    要看你打算在什么控件的什么事件中得到
      

  5.   

    我是这个写的 DropDownList ddlBigClass = GridView1.SelectedRow.Cells[3].FindControl("drop1") as DropDownList;
     但是程序说的是 未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
      

  6.   

    我打算在更新的时间得到里面的数据来更新数据库,还有
    asp:DropDownList 的  Enabled="false" 我要在点了更新之后给他改为 true 但不会成功
      

  7.   

    gridview 外边添加一个按钮  按钮的事件 是
    protected void Button3_Click(object sender, EventArgs e)
        {
            string strDropDownListSelectValue = string.Empty;
            int x = GridView1.Rows.Count;
            for (int i = 0; i < x; i++)
            {
                strDropDownListSelectValue += ((DropDownList)GridView1.Rows[i].Cells[0].FindControl("DropDownList1")).SelectedValue+"("+i.ToString()+")"+"---------";
                
            }
            Response.Write(strDropDownListSelectValue);
            //Response.Write(x.ToString());
            Response.End();
        }
      

  8.   

    值我能取到了,但怎么给他赋值呀.
    我的 asp:DropDownList 的  Enabled="false" 我要在点了更新之后给他改为 true
      

  9.   

    DropDownList drop = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drop1"); 
    string zhi = drop.selectValue;
    可以取到的!
    要是要改变Enabled,利用Ajax更简单
      

  10.   


    你的事件顺序错误,你要变更DropDownlist的值要在editing事件中
    第一步:先在GridView的行editing事件中使((DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drop1")).enable=true;
     
    第二步:在GridView的行update事件中获取DropDownList的值((DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drop1")).SelectedValue;
      

  11.   


    GridView1.Rows[i].Cells[0].Enabled = False
      

  12.   

    第一步:先在GridView的行editing事件中使((DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drop1")).enable=true; 
    这样做了,但不不能生效的,不能给他赋值呀.
      

  13.   

    cfreez 谢谢你的回答哈,不过我是需要把 asp:DropDownList  改为 true
    <asp:TemplateField HeaderText="机型">
                        <ItemTemplate>
                        <asp:DropDownList runat="server" ID="drop1"  DataSourceID="ObjectDataSource2"  DataTextField="M_type"  DataValueField="id" SelectedValue='<%# Bind("m_id") %>' Enabled="false" ></asp:DropDownList>                    
                        </ItemTemplate>
                        </asp:TemplateField>
      

  14.   


      protected void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
            {
                DropDownList ddlBigClass = GridView1.Rows[e.RowIndex].FindControl("drop1") as DropDownList;
                ObjectDataSource1.UpdateParameters["M_id"].DefaultValue = ddlBigClass.SelectedValue;
                
            }
            protected void CustomersGridView_RowEditing(Object sender, GridViewEditEventArgs e)
            {            DropDownList ddlBigClass = GridView1.Rows[e.NewEditIndex].FindControl("drop1") as DropDownList;
                ddlBigClass.Enabled = false;
            }
      

  15.   


    应该这样:protected void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
            {
                DropDownList ddlBigClass = GridView1.Rows[e.RowIndex].Cells[你的单元格索引].FindControl("drop1") as DropDownList;
                ObjectDataSource1.UpdateParameters["M_id"].DefaultValue = ddlBigClass.SelectedValue;
                
            }
            protected void CustomersGridView_RowEditing(Object sender, GridViewEditEventArgs e)
            {            DropDownList ddlBigClass = GridView1.Rows[e.NewEditIndex].Cells[你的单元格索引].FindControl("drop1") as DropDownList;
                ddlBigClass.Enabled = false;
            }
      

  16.   

    哦,上面的不对,是这样的
    protected void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
            {
                DropDownList ddlBigClass = GridView1.Rows[e.RowIndex].Cells[你的单元格索引].FindControl("drop1") as DropDownList;
                ObjectDataSource1.UpdateParameters["M_id"].DefaultValue = ddlBigClass.SelectedValue;
                
            }
            protected void CustomersGridView_RowEditing(Object sender, GridViewEditEventArgs e)
            {            DropDownList ddlBigClass = GridView1.Rows[e.NewEditIndex].Cells[你的单元格索引].FindControl("drop1") as DropDownList;
                ddlBigClass.Enabled = false;
            }