前端控件:
復(fù)制代碼 代碼如下:
label>發(fā)布欄目:asp:DropDownList ID="sectionDropDownList" runat="server">/asp:DropDownList>/label
數(shù)據(jù)綁定:
復(fù)制代碼 代碼如下:
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事件:
復(fù)制代碼 代碼如下:
string newsTitle = sectionDropDownList.SelectedValue;
Response.Write(newsTitle);
問(wèn)題分析:
因?yàn)樵趐age_load中每次都綁定了數(shù)據(jù)源,而去調(diào)用Button事件時(shí),實(shí)際是每次都刷新了頁(yè)面的,于是每次在打印出來(lái)前都是初始化的值,于是每次都是輸出的的一個(gè)值。
問(wèn)題解決:
判斷是否是頁(yè)面回調(diào)。
前端控件:
復(fù)制代碼 代碼如下:
label>發(fā)布欄目:asp:DropDownList ID="sectionDropDownList" runat="server">/asp:DropDownList>/label
數(shù)據(jù)綁定:
復(fù)制代碼 代碼如下:
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事件:
復(fù)制代碼 代碼如下:
string newsTitle = sectionDropDownList.SelectedValue;
Response.Write(newsTitle);