我有两个名为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));
}
}
请告诉我正确的方法。 因此,如果我选择羽毛球,那么所有的设备都会显示在第二个下拉列表中,就像国家和城市一样。
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>
答案 0 :(得分:0)
你的代码没有错。如果您不使用数据库 只是把
Category.Items.Clear();
如下
protected void Category_SelectedIndexChanged(object sender, EventArgs e)
{
Category.Items.Clear();
if (SportsName.SelectedIndex == 1)
{
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.Add(new ListItem("Golf", string.Empty));
}
if (SportsName.SelectedIndex == 3)
{
Category.Items.Add(new ListItem("Ball", string.Empty));
}
}
位于您的函数顶部。因此,您不需要在每个if else条件下编写它
但是你是否使用数据库。
它应该如下
<tr>
<td class="style9">
Sports Name</td>
<td>
<asp:DropDownList ID="SportsName" runat="server" AutoPostBack="True"
onselectedindexchanged="Category_SelectedIndexChanged" 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"
Width="126px">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
答案 1 :(得分:0)
您没有在html代码中设置选择的索引更改事件。进行如下更改。 替换此代码
<asp:DropDownList ID="SportsName" runat="server" AutoPostBack="True" Height="26px" Width="126px">
</asp:DropDownList>
使用此代码
<asp:DropDownList ID="SportsName" runat="server" SelectedIndexChanged="Category_SelectedIndexChanged"
AutoPostBack="True" Height="26px" Width="126px">
</asp:DropDownList>
你完成了所有工作,但你放弃了在html代码中放置SelectedIndexChange事件方法。
看起来像是一个家庭作业。