[英]Dropdownlist in ASP.net C#
我有兩個dropdownlist's.DropDownList2(未綁定到數據源)和DropDownList3(已綁定到數據源)
更改一個下拉列表中的輸入后,另一下拉列表中的某些內容應更改。 為此,我使用了邏輯。
這兩個控件都啟用了自動回發。
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList2.SelectedItem.Text == "Stamp")
{
DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STA"));
DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STM"));
}
<asp:DropDownList ID="DropDownList3" runat="server"
DataSourceID="SqlDataSource1" DataTextField="skey" DataValueField="casecode"
AppendDataBoundItems="True" AutoPostBack="True">
<asp:ListItem Selected="True" Value="S">Select</asp:ListItem>
</asp:DropDownList>
現在的問題是,當我選擇DropDownList2.SelectedItem.Text == "Reg"
STA和STM不存在時。 我希望在選擇“ Reg”時在下拉列表中返回STA和STM值。
當我第一次加載頁面並選擇“ Reg”時,存在DropDownList3中的所有值(包括“ STA”和“ STM”),並且比當我選擇“ Stamp”時,值“ STA”和“ STM”都丟失了(如圖所示)在代碼中)。 現在,當我再次選擇“ Reg”時,此值不存在,我希望該值再次出現。
我需要做什么?? 我是否必須再次將其綁定到數據庫?
還有其他以其他方式使用的邏輯嗎?如果有人可以幫助我
您可以在每次DropDownList2選定的索引更改時綁定DropDownList3,然后僅當值為“ Stamp”時,才從DropDownList3中刪除值“ STA”和“ STM”
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
// Fill DropDownList3 data source and bind it again to restore all the items
FillDataSource(); // This method gets all the data from DropDownList3
DropDownList3.DataBind();
if (DropDownList2.SelectedItem.Text == "Stamp")
{
DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STA"));
DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STM"));
}
...
如果您知道下拉菜單項的值,則可以將它們添加到else子句中,如果您不知道必須重新綁定值/文本組合。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.