<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();
Button事件:
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();
}
Button事件:
string newsTitle = sectionDropDownList.SelectedValue;
Response.Write(newsTitle);