【问题标题】:Database Driven Dependant Dynamic DropDownList on ASP.NETASP.NET 上的数据库驱动的依赖动态下拉列表
【发布时间】:2015-08-29 06:52:03
【问题描述】:

我的 AddProduct.aspx 中有 2 个下拉列表,如下所示

<asp:DropDownList ID="ddlBrand" runat="server"></asp:DropDownList>    
<asp:DropDownList ID="ddlSubBrand" runat="server"></asp:DropDownList>

在文件后面的代码中,我使用这些代码来让我的品牌下拉列表成为数据库驱动的

ddlBrand.DataSource = brandManager.getAllBrand();
            ddlBrand.DataTextField = "BrandName";
            ddlBrand.DataValueField = "BrandId";
            ddlBrand.DataBind();
            ListItem item = new ListItem();
            item.Value = "0";
            item.Text = "--Select Brand--";
            ddlBrand.Items.Insert(0, item);

我试图让我的子品牌下拉列表是动态的,并根据品牌下拉列表中选择的内容进行更改。两个下拉列表彼此相邻,因此我不确定如何在选择品牌后立即获得所选品牌的价值。我计划在我的类中使用从数据库中检索值的这条语句将选定的值解析到数据库中。

   "SELECT SubBrandId,SubBrandName " +
   " DateOfBirth FROM SubBrand WHERE " +
   " BrandId = @inBrandId"

【问题讨论】:

    标签: c# mysql asp.net dynamic drop-down-menu


    【解决方案1】:

    一旦品牌被选中,您可能希望回复所选商品:

    <asp:DropDownList ID="ddlBrand" runat="server" OnSelectedIndexChanged="BrandSelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
    

    在代码隐藏中,您必须添加方法:

    protected void BrandSelectedIndexChanged(object sender, EventArgs args) {
        int id;
        if (int.TryParse(ddlBrand.SelectedValue, out id)) {
            //Do your thing here with the id of the brand, e.g. bind sub brands.
        }
    }
    

    【讨论】:

    • 我到家后会与您联系。似乎它会起作用。非常感谢:)
    猜你喜欢
    • 2017-11-22
    • 2020-11-23
    • 2020-06-28
    • 1970-01-01
    • 2012-05-13
    • 2018-06-25
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    相关资源
    最近更新 更多