两个页面:
plist.aspx 搜索显示页面,在该页面的左侧有个搜索区,引用的是search.ascx;
search.ascx 有个城市的列表框控<asp:DropDownList ID="CityID" runat="server" onselectedindexchanged="CityID_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>,并该页面对其绑定数据,还有个<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/btn_search.gif" PostBackUrl="~/plist.aspx" Height="22px" Width="50px"/>
问题:
如何在plist.aspx.cs中获得DropDownList中用户选中的值?
我是在plist.aspx.cs中这样写的
[code=C#]
DropDownList cddl = (DropDownList)FindControl("CityID");
Response.Write(cddl.SelectedValue.ToString());
但是报错:System.NullReferenceException: 未将对象引用设置到对象的实例。谢谢

解决方案 »

  1.   

    实际情况就是这样,我也没办法了...plist.aspx<%@ Register src="User_Control/search.ascx" tagname="search" tagprefix="uc2" %>
    页面中引用的如下:
    <uc2:search ID="search1" runat="server" />
    能换个方法吗
      

  2.   

    DropDownList cddl = (DropDownList)FindControl("CityID"); 这句你是在哪找这个列表的?说明找不到那个下拉列表框啊!
    换种方法获取列表框!
      

  3.   

    问题找到了在页面被解析成HTML后search.ascx中的DropDownList 的ID变成了search1$CityID 如下:<select name="search1$CityID" onchange="javascript:setTimeout('__doPostBack(\'search1$CityID\',\'\')', 0)" id="search1_CityID">
    <option value="">==请选择城市==</option>
    </select>
    因此要在plist.aspx.cs中使用:DropDownList cddl = (DropDownList)FindControl("search1$CityID");
    可否在plist.aspx中解析成html后依然保留DropDownList 的ID值?
      

  4.   

    CityID.ClientId;
    也可使用
     this.CityID.Attributes.Add("onchange", "Result()");
    在JS里取值
      

  5.   


    在plist.aspx.cs中已经没有CityID了DropDownList cddl = (DropDownList)FindControl("CityID")
    cddl是null值在search.ascx中是可以了,现在我不在search.ascx.cs中获得CityID中的值,是在plist.aspx.cs中获得.CityID.ClientId怎么使用;
    能否给个事例
      

  6.   

    ID号变了 ,你需要点击浏览  然后查看源码 看哪个控件的ID是好多.
    还有一种方法 就是get set然后赋值  取值
      

  7.   

    DropDownList list = new DropDownList();
                foreach (Control c in this.WebUserControl11.Controls)
                {
                    if (c is DropDownList)
                    {
                        list = c as DropDownList;
                        break;
                    }
                }
                Response.Write(list.SelectedValue.ToString());
      

  8.   

    参考:
    http://blog.csdn.net/insus/archive/2008/01/21/2057539.aspx
    或者:
    http://www.cnblogs.com/insus/articles/1431940.html