【问题标题】:Dynamically generating a DropdownLIst control from an array populating with Text and Values从填充有文本和值的数组动态生成 DropdownLIst 控件
【发布时间】:2023-03-11 03:16:01
【问题描述】:

我正在尝试将 DropDownList 控件绑定到各种数据存储。在我的情况下,数据存储是数组文件值和项目文本。

这是我的 HTML 的一个简短示例:

 <asp:DropDownList ID="DropDownListDealCategory"  
                                          runat="server" 
                                          Height="25px" 
                                          Width="150px" 
                                          AutoPostBack="true"
                                          OnSelectedIndexChanged="DropDownListDealCategory_SelectedIndexChanged">
                            <asp:ListItem Selected="True" Value="0">-- Select Category --</asp:ListItem>
                            <asp:ListItem Value="10">Electronics</asp:ListItem>
                            <asp:ListItem Value="22">Computer</asp:ListItem>                               
                        </asp:DropDownList>

<asp:DropDownList ID="DropDownSubCategories" 
                                            runat="server" 
                                            Visible="false" 
                                            Height="25px" 
                                            Width="170px" 
                                            AutoPostBack="true"
                                            OnSelectedIndexChanged="DropDownSubCategories_SelectedIndexChanged">
                        </asp:DropDownList>

(C#) 后面的代码将从两个数组动态生成一个 DropDownList 控件。

    protected void DropDownDealCategory_SelectedIndexChanged(object sender, EventArgs e)
{
string[] Electronics = new[] { " Select Subcategory ", "Cameras and Photography", "Home Audio"};
string[] Computer = new[] {" Select Subcategory " "Laptops", "Monitors"};

if (DropDownListCategory.SelectedItem.Text == "Electronics")
    {
        DropDownSubCategories.DataSource = Electronics;
    }
 else if (DropDownListDealCategory.SelectedItem.Text == "Computer")
    {
        DropDownSubCategories.DataSource = Computer;
    }

DropDownSubCategories.DataBind();
    DropDownSubCategories.Visible = DropDownListCategory.SelectedItem.Text != " Select Category ";

所以,到这里为止一切都很好,除了我还需要 DropDownSubCategories DropDownList 控件中的每个项目都需要一个 categoryId,否则我将无法从外部数据库中检索任何产品,因为我需要一个 name + categoryid 来显示产品描述。

我的问题是,有没有办法将两个值(一个作为 Value,另一个作为 Item.Text )添加到数组中,这样我就可以将两者都绑定到 DropDownList 控件?

谢谢,任何帮助甚至替代建议都将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    尝试使用ListItems。应该如下所示。

            var Electronics = new[]{
                new ListItem{ Value = "0", Text = " Select Subcategory "},
                new ListItem{ Value = "1", Text = "Cameras and Photography"},
                new ListItem{ Value = "2", Text = "Home Audio"},
            };
    

    您可能需要添加 using。

    using System.Web.UI.WebControls;
    

    编辑:

    尝试以这种方式添加它们:

    DropDownSubCategories.Items.Clear();
    DropDownSubCategories.Items.AddRange(Electronics);
    

    或者如果AddRange() 不是一个函数,单独来说:

    DropDownSubCategories.Items.Add(new ListItem{ Value = "2", Text = "Home Audio"});
    

    等等。

    【讨论】:

    • 使用这种方法,我只能得到 Text 的值。
    • 因为 DataBind 方法仅在设置 DataValueField 属性时才绑定值。如果在调用 DataBind 之前将 DataValueField 属性设置为“Value”,则您的值将出现在标记上。您还需要将 DataTextField 属性设置为“文本”。这是因为数据绑定和手动添加项目的工作方式不同。数据绑定不知道 ListItem 类型的存在,并通过评估数据源中的项目来生成标记。或者只是将它们添加为项目
    • @TomasJimenez 不幸的是,我使用 MVC,所以它略有不同。你可能不得不玩弄它。
    • 非常感谢!你明白了!
    【解决方案2】:

    Dictionary 类旨在存储键/值对。

                Dictionary<int, string> Electronics = new Dictionary<int, string>() {
    
                {0, "Select Subcatagory" },
                {1, "Cameras and Photography" },
                {2, "Home Audio" }
    
            };
    

    【讨论】:

    • 我创建了一个测试标签 (LabelCat) 来查看这两种方法的结果,您的方法和“蓝眼巨兽”建议的方法。 LabelCat.Text = DropDownListSubCategories.SelectedValue;我得到了这个结果[23,放大器]。所以,不是很实用。不过,感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多