我有一個主頁DropDownList
和2個其他頁面。使其選擇打印報告的設置。
問題是 - 我在這些DropDownList
s中使用DataSource
s,但它們沒有空值。我應該在哪裏爲DropDownList
添加一個新的空值以正確工作?
有我的代碼(在評論是,我需要添加的事情)我應該在哪裏爲DropDownList添加新的ListItem
protected void ddlMain_Load(object sender, EventArgs e)
{
switch (ddlMain.SelectedValue)
{
case "1":
dFirst.Visible = true;
dSecond.Visible = false;
break;
case "2":
dFirst.Visible = false;
dSecond.Visible = true;
break;
}
<div>
<asp:DropDownList ID="ddlMain" runat="server" AutoPostBack="true" OnLoad="ddlMain_Load">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2 " Value="2"></asp:ListItem>
</asp:DropDownList>
</div>
<div runat="server" id="dFirst" visible="false">
<asp:DropDownList ID="ddlFirst" runat="server" DataSourceID="sdsFirst"
DataTextField="name" DataValueField="id" OnLoad="ddlFirst_Load">
</asp:DropDownList>
<asp:SqlDataSource ID="sdsFirst" runat="server"
ConnectionString="<%$ ConnectionStrings:conStr %>">
</asp:SqlDataSource>
</div>
//ddlFirst.Items.Add(new ListItem("--Select--", "-1"));
我試圖OnLoad
次要元素,OnLoad
主要DropDownList
和Page_Load
。它們不適合我,因爲新頁面ListItem
在頁面首次加載時不會顯示,或在頁面刷新時添加超過1次。
什麼是空值? – Imad
對不起,我更新了我的問題!示例是註釋在代碼部分。 –