两个页面:
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: 未将对象引用设置到对象的实例。谢谢
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: 未将对象引用设置到对象的实例。谢谢
页面中引用的如下:
<uc2:search ID="search1" runat="server" />
能换个方法吗
换种方法获取列表框!
<option value="">==请选择城市==</option>
</select>
因此要在plist.aspx.cs中使用:DropDownList cddl = (DropDownList)FindControl("search1$CityID");
可否在plist.aspx中解析成html后依然保留DropDownList 的ID值?
也可使用
this.CityID.Attributes.Add("onchange", "Result()");
在JS里取值
在plist.aspx.cs中已经没有CityID了DropDownList cddl = (DropDownList)FindControl("CityID")
cddl是null值在search.ascx中是可以了,现在我不在search.ascx.cs中获得CityID中的值,是在plist.aspx.cs中获得.CityID.ClientId怎么使用;
能否给个事例
还有一种方法 就是get set然后赋值 取值
foreach (Control c in this.WebUserControl11.Controls)
{
if (c is DropDownList)
{
list = c as DropDownList;
break;
}
}
Response.Write(list.SelectedValue.ToString());
http://blog.csdn.net/insus/archive/2008/01/21/2057539.aspx
或者:
http://www.cnblogs.com/insus/articles/1431940.html