2

我有两个名为 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>
                &nbsp;</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>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style9">
                Sub Category</td>
            <td>
                <asp:DropDownList ID="SubCategory" runat="server" Height="26px" Width="126px">
                </asp:DropDownList>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style9">
                Brand</td>
            <td>
                <asp:DropDownList ID="Brand" runat="server" Height="26px" Width="128px">
                </asp:DropDownList>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style9">
                Image</td>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style9">
                MRP</td>
            <td>
                <asp:TextBox ID="mrp" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style9">
                Our Price</td>
            <td>
                <asp:TextBox ID="ourprice" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style9">
                You Save</td>
            <td>
                <asp:TextBox ID="yousave" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style9">
                Shipping Charges</td>
            <td>
                <asp:TextBox ID="shippingcharges" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style9">
                Expected Delivery</td>
            <td>
                <asp:TextBox ID="expecteddelivery" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style9">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="submit" runat="server" Text="Button" />
            </td>
            <td>
                <asp:Button ID="reset" runat="server" Text="Button" />
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style9">
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
</asp:Content>
4

2 回答 2

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>
            &nbsp;</td>
    </tr>
    <tr>
        <td class="style9">
            Category</td>
        <td>
            <asp:DropDownList ID="Category" runat="server" Height="26px" 
                 Width="126px">
            </asp:DropDownList>
        </td>
        <td>
            &nbsp;</td>
    </tr>
于 2012-11-18T06:19:32.220 回答
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>

您已经完美地完成了所有工作,但您放弃了将 SelectedIndexChange 事件方法放入 html 代码中。
它看起来像一个家庭作业。

于 2012-11-18T06:52:54.517 回答