我有个.aspx页面 这个页面里套用一个母版页 在页面的的Poad_Load方法中 if(!IsPostBack){//绑定下拉框()}
在按钮的点击事件中
protected void BtnAdd_Click(object sender, EventArgs e)
{
            string userId = DDLParent.SelectedItem.Value.ToString();
            Response.Write(userId.ToString());
}
userId取到是null值 
报错未将对象引用实例跟踪代码时 DDLParent.SelectedItem.Value值是null 问一下 怎么能取到下拉框中选择的值

解决方案 »

  1.   

    DDLParent.SelectedValue即可取值
    问题是你绑定的时候绑的有值没?
      

  2.   

    !~你看到的不是value是text,你这么取DDLParent.SelectedItem.Text看是否有值
      

  3.   

    也没有值 也是null值 也报错 未将实例引用对象 
      

  4.   

     你看的是TEXT的值,你看下value里面,VALUE 肯定没数据,你没绑定,所以为空!
      

  5.   

    DDLParent.DataSource = dr1; //数据源
    DDLParent.DataValueField = "id"; //你应该是这一句没有写上去吧
    DDLParent.DataTextField = "name";
    DDLParent.DataBind();
      

  6.   

      string userId = DDLParent.SelectedValue.ToString();
      

  7.   

    private void BindRoleParent()
            {
                string strSelParent = "select xxx from xxx where ROLE_PARENTID =0";
                DataTable dtcm6008 = SystemUserDa.GetDataInfo(strSelParent);
                if (dtcm6008.Rows.Count > 0)
                {
                    ListItem item = new ListItem("请选择", "0");
                    DDLParent.Items.Add(item);
                    for (int i = 0; i < dtcm6008.Rows.Count; i++)
                    {
                        item = new ListItem(dtcm6008.Rows[i]["ROLE_NAME"].ToString(), dtcm6008.Rows[i]["SEQ_CM6008"].ToString());
                        DDLParent.Items.Add(item);
                    }
                }
                else
                {
                    ListItem item = new ListItem("请选择", "0");
                    DDLParent.Items.Add(item);
                }
            }
      

  8.   

    试问 你确定value绑定的有值?
      

  9.   

    先搞明白你要取得是value还是text还是index。然后再根据需要取值
      

  10.   

    <select name="ctl00$ContentPlaceHolder1$DDLParent" id="ctl00_ContentPlaceHolder1_DDLParent">
    <option value="0">请选择</option>
    <option value="88">飞行员领导1</option>
    <option value="90">飞行员2</option>
    <option value="89">飞行员1</option>
     
    </select>
      

  11.   

    select 取值应该是Request.Form["selectName"].ToString()吧
      

  12.   

    单步调试了 在按钮的事件中string userId = DDLParent.SelectedValue.ToString();为null不加母版页的话 就没有问题 是不是和母版页有关
      

  13.   

    string userId = DDLParent.SelectedValue.ToString();
    //这样肯定取不到值,string userId = DDLParent.SelectedItem.Value.ToString();//这样才可以的
      

  14.   

    string userId = DDLParent.SelectedItem.Value.ToString();
    这样写也试过 DDLParent value text index都是空目前只有把这个页面引用的模板页 给去掉 才能获得值 暂时没有别的办法 
    如果有哪位高手能有办法 请继续留言我会关注的
      

  15.   

    string userId = DDLParent.SelectedValue
    为什么取不了值?
      

  16.   

    this.Page.FindControl("drop")as DropDownList   这个样子来获取值,看能不能获取到!!!
      

  17.   

    this.Page.FindControl("drop")as DropDownList
    貌似也不行
      

  18.   

    请把你的绑定代码写在这前面  string userId = DDLParent.SelectedItem.Value.ToString();
      Response.Write(userId.ToString());
    }
      

  19.   

    看来是无招了...模板里,Findcontrol也取不到。汗
      

  20.   

    绑定下拉列表数据的代码    是否放在 !IspostBack里面呀
    如果不是 你点击按钮回发页面  首先进入Page_Load 里面  再到click事件里面
    没写在 !IspostBack 的话  相当于又重新绑定了  你在click 里面DDLParent.SelectedValue 肯定就报未将对象引用到实例了
      

  21.   

    BindRoleParent()方法,就if(!IsPostBack)里调用了吗
      

  22.   

    恩 是的 if(!IsPostBack){BindRoleParent();}在里面 
      

  23.   

    谁说这样不行,尝试过莫 string userId=DDLParent.SelectedValue.ToString();
    dropdownlist绑定值得问题,看看值绑定上没有,只要dropdownlist绑定上值了,这样取Value一点问题都没有
      

  24.   

    你用jquery ajax去取值再传递到后台 啊
      

  25.   

    绑定换种简单的方式吧 搞得那么复杂
                  DDLParent.DataSource = dt;//dt是datetable 你懂的
                DDLParent.DataTextField = "name";
                DDLParent.DataValueField = "id";
                DDLParent.DataBind();
                DDLParent.Items.Insert(0, new ListItem("请选择", "0"));
    获取方式
    string userId=DDLParent.SelectedValue
    还不行的话找微软吧