更新时间:2023-10-15 22:15:16
不要让这个变得复杂。有一种非常简单的方法可以使用 AjaxCascadingDropDown
。
参考 - ASP.Net中的AJAX级联DropDown示例 [ ^ ]
Don't make this complicated. There is a very easy way of doing this using AjaxCascadingDropDown
.
Refer - AJAX Cascading DropDown Example in ASP.Net[^]
而不是使用单个updatepanel使用updatepanel和每个下拉列表,请认为它可以正常工作。
Hi,
Instead of using a single updatepanel use updatepanel with every dropdown, think so it will work fine.
尝试使用单个更新面板....
设计师:
Try using Single Update Panel....
DESIGNER:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Learn How Add ASP.NET 3.5 AJAX UpdatePanel Triggers</title></head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Country"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<br />
<asp:Label ID="Label2" runat="server" Text="State"></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
Enabled="False" onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
<br />
<asp:Label ID="Label3" runat="server" Text="City"></asp:Label>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True"
Enabled="False" onselectedindexchanged="DropDownList3_SelectedIndexChanged">
</asp:DropDownList>
<br />
<asp:Label ID="Label4" runat="server"></asp:Label>
<br />
<br />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
背后的代码:
CODE BEHIND:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<string> l1 = new List<string>();
l1.Add(" ");
l1.Add("India");
l1.Add("USA");
foreach (string item in l1)
{
DropDownList1.Items.Add(item);
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Enabled = true;
DropDownList3.Enabled = false;
Label4.Text = "";
List<string> l2 = new List<string>();
if (DropDownList1.SelectedItem.Text == "India")
{
DropDownList2.Items.Clear();
l2.Add(" ");
l2.Add("Maharashtra");
l2.Add("Goa");
foreach (string item in l2)
{
DropDownList2.Items.Add(item);
}
}
else
{
DropDownList2.Items.Clear();
l2.Add(" ");
l2.Add("Indiana");
l2.Add("Texas");
foreach (string item in l2)
{
DropDownList2.Items.Add(item);
}
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList3.Enabled = true;
List<string> l3 = new List<string>();
if (DropDownList2.SelectedItem.Text == "Maharashtra")
{
DropDownList3.Items.Clear();
l3.Add(" ");
l3.Add("Pune");
l3.Add("Mumbai");
foreach (string item in l3)
{
DropDownList3.Items.Add(item);
}
}
else if (DropDownList2.SelectedItem.Text == "Goa")
{
DropDownList3.Items.Clear();
l3.Add(" ");
l3.Add("Panji");
l3.Add("Vasco");
foreach (string item in l3)
{
DropDownList3.Items.Add(item);
}
}
else if (DropDownList2.SelectedItem.Text == "Indiana")
{
DropDownList3.Items.Clear();
l3.Add(" ");
l3.Add("Wilkinson");
l3.Add("Morocco");
foreach (string item in l3)
{
DropDownList3.Items.Add(item);
}
}
else if (DropDownList2.SelectedItem.Text == "Texas")
{
DropDownList3.Items.Clear();
l3.Add(" ");
l3.Add("Archer City");
l3.Add("Bryan");
foreach (string item in l3)
{
DropDownList3.Items.Add(item);
}
}
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
Label4.Text = DropDownList1.SelectedItem.Text.ToString() + " " + DropDownList2.SelectedItem.Text.ToString() + " " + DropDownList3.SelectedItem.Text.ToString();
}
}