我有两个名为 SportsName 和 Category 的下拉列表。我希望类别列表依赖于 SportsName 列表中的选定项目。这是我用来执行此操作的代码,但它不起作用
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SportsName.Items.Insert(0,new ListItem("Select", string.Empty));
SportsName.Items.Insert(1,new ListItem("Badminton", "Badminton"));
SportsName.Items.Insert(2,new ListItem("Tennis", string.Empty));
SportsName.Items.Insert(3,new ListItem("Table Tennis", string.Empty));
SportsName.Items.Insert(4,new ListItem("Swimming", string.Empty));
SportsName.Items.Insert(5,new ListItem("Sports Ball", string.Empty));
SportsName.Items.Insert(6,new ListItem("Bats", string.Empty));
SportsName.Items.Insert(7,new ListItem("Sports Cap", string.Empty));
SportsName.Items.Insert(8,new ListItem("Gym Equipments", string.Empty));
SportsName.Items.Insert(9,new ListItem("Darts", string.Empty));
SportsName.Items.Insert(10,new ListItem("Billards", string.Empty));
SportsName.Items.Insert(11,new ListItem("Fitness", string.Empty));
SportsName.Items.Insert(12,new ListItem("Sports Medicine", string.Empty));
SportsName.Items.Insert(13,new ListItem("Contact Sports", string.Empty));
SportsName.Items.Insert(14,new ListItem("Outdoor", string.Empty));
SportsName.Items.Insert(15,new ListItem("LifeSyle", string.Empty));
SportsName.Items.Insert(16,new ListItem("Shoes and Apprel ", string.Empty));
SportsName.Items.Insert(17,new ListItem("Hockey", string.Empty));
SportsName.Items.Insert(18,new ListItem("Golf", string.Empty));
SportsName.SelectedIndex=0;
}
}
protected void Category_SelectedIndexChanged(object sender, EventArgs e)
{
if (SportsName.SelectedIndex == 1)
{
Category.Items.Clear();
Category.Items.Add(new ListItem("Rackets", string.Empty));
Category.Items.Add(new ListItem("Sets", string.Empty));
Category.Items.Add(new ListItem("Shuffle", string.Empty));
}
if (SportsName.SelectedIndex == 2)
{
Category.Items.Clear();
Category.Items.Add(new ListItem("Golf", string.Empty));
}
if (SportsName.SelectedIndex == 3)
{
Category.Items.Clear();
Category.Items.Add(new ListItem("Ball", string.Empty));
}
}
请告诉我正确的方法来做到这一点。因此,如果我选择 Badminton,那么它的所有设备都会显示在第二个下拉列表中,就像在国家和城市的情况下一样。
html代码
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.style8
{
width: 100%;
}
.style9
{
width: 215px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table class="style8">
<tr>
<td class="style9">
Sports Name</td>
<td>
<asp:DropDownList ID="SportsName" runat="server" AutoPostBack="True"
Height="26px" Width="126px">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td class="style9">
Category</td>
<td>
<asp:DropDownList ID="Category" runat="server" Height="26px"
onselectedindexchanged="Category_SelectedIndexChanged" Width="126px">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td class="style9">
Sub Category</td>
<td>
<asp:DropDownList ID="SubCategory" runat="server" Height="26px" Width="126px">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td class="style9">
Brand</td>
<td>
<asp:DropDownList ID="Brand" runat="server" Height="26px" Width="128px">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td class="style9">
Image</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
<td>
</td>
</tr>
<tr>
<td class="style9">
MRP</td>
<td>
<asp:TextBox ID="mrp" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style9">
Our Price</td>
<td>
<asp:TextBox ID="ourprice" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style9">
You Save</td>
<td>
<asp:TextBox ID="yousave" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style9">
Shipping Charges</td>
<td>
<asp:TextBox ID="shippingcharges" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style9">
Expected Delivery</td>
<td>
<asp:TextBox ID="expecteddelivery" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style9">
<asp:Button ID="submit" runat="server" Text="Button" />
</td>
<td>
<asp:Button ID="reset" runat="server" Text="Button" />
</td>
<td>
</td>
</tr>
<tr>
<td class="style9">
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</asp:Content>