<label>發(fā)布欄目:<asp:DropDownList ID="sectionDropDownList" runat="server"></asp:DropDownList></label
數(shù)據(jù)綁定:
代碼如下:
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中每次都綁定了數(shù)據(jù)源,而去調(diào)用Button事件時,實際是每次都刷新了頁面的,于是每次在打印出來前都是初始化的值,于是每次都是
<label>發(fā)布欄目:<asp:DropDownList ID="sectionDropDownList" runat="server"></asp:DropDownList></label
數(shù)據(jù)綁定:
代碼如下:
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);
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com