asp.net中关于dropdwonlist无法获得值问题
<label>发布栏目:<asp:DropDownList ID="sectionDropDownList" runat="server"></asp:DropDownList></label
SourceDb DropDwonListData = new SourceDb();
string DropDwonSelect = "SELECT * FROM [Section]";
sectionDropDownList.DataSource = DropDwonListData.DatasetDb(DropDwonSelect).Tables[0].DefaultView;
sectionDropDownList.DataTextField = "name";
sectionDropDownList.DataValueField = "code";
sectionDropDownList.DataBind();
string newsTitle = sectionDropDownList.SelectedValue;
Response.Write(newsTitle);
问题分析:
因为在page_load中每次都绑定了数据源,而去调用Button事件时,实际是每次都刷新了页面的,于是每次在打印出来前都是初始化的值,于是每次都是输出的的一个值。
问题解决:
判断是否是页面回调。
<label>发布栏目:<asp:DropDownList ID="sectionDropDownList" runat="server"></asp:DropDownList></label
if(!IsPostBack){
SourceDb DropDwonListData
= new SourceDb();
string DropDwonSelect = "SELECT * FROM [Section]";
sectionDropDownList.DataSource = DropDwonListData.DatasetDb(DropDwonSelect).Tables[0].DefaultView;
sectionDropDownList.DataTextField = "name";
sectionDropDownList.DataValueField = "code";
sectionDropDownList.DataBind();
}
string newsTitle = sectionDropDownList.SelectedValue;
Response.Write(newsTitle);