【问题标题】:ASP.NET: Dynamically creating controlsASP.NET:动态创建控件
【发布时间】:2011-08-28 22:41:29
【问题描述】:

我有以下场景:

我的页面有一个下拉列表,允许用户选择一个类别。 对于每个类别,都有一组属性,其值应从用户那里获取。每个类别的属性数量不同。 根据用户选择的类别,应根据属性创建一组下拉列表,并填写相应的属性值。

由于要求页面不应该重新加载,我计划使用 AJAX (?) 获取数据(来自 SQL Server 2008)。我是 ASP.NET 的新手,虽然我对 C# 很熟悉,但没有使用过 AJAX。需要有关如何进行的建议。

编辑:如果我需要动态生成组合框,this 有用吗?

【问题讨论】:

  • 你的javascript怎么样?因为这就是 AJAX 所需要的。您可以将 ASP.Net AJAX 工具包与 UpdatePanel 一起使用,但它会变得相当麻烦。好处是你不需要真正了解javascript。
  • 嗯,这可能会妨碍你。如果时间紧迫,您可以使用 ASP AJAX UpdatePanel 控件msdn.microsoft.com/en-us/library/bb399001.aspx。您需要知道的大部分内容都是在 c# 中完成的。不过我建议你学习 javascript 和 jQuery(或其他框架),因为它对你在 web 开发中的好处是无限的。
  • 根据您的编辑,是的,这很有用,但它是 DDL 的静态列表。每个中列出的内容会发生变化,但实际控件本身不会发生变化。

标签: c# asp.net ajax sql-server-2008


【解决方案1】:

您可以使用UpdatePanelPageMethods

在这两种情况下,我想说的是,当您想使用 AJAX 并制作动态 Web 应用程序时,您确实需要了解 JavaScript。这需要一些时间,但有回报,不用担心。

你可以在这里搜索关于PageMethod的SO,例如看这个:

Regarding PageMethod in asp.net

【讨论】:

    【解决方案2】:

    您可以使用以下方法(如果您不喜欢使用 javascript 构建更复杂的 UI)。

    它的工作原理是在页面加载时动态创建属性 DropDownLists(您将根据数据库查询的结果实现它)并隐藏每个属性,以便稍后显示。

    在选择了一个类别后,正确的 DropDownLists 就会变得可见(这里的查询再次可以确定哪个属性 DropDownLists 变得可见)。

    显然,它可能需要一些修改才能生成一个包含每个 DropDownList 和一个标签控件的面板,而不是仅仅创建一些 DropDownLists。

    然后您将显示/隐藏面板而不是 DropDownList,但希望它为您指明正确的方向。

    希望这会有所帮助。

    标记

    <style type="text/css">
        select
        {
            display:block;
            margin-top:10px;
        }
    </style>
    
    ....
    
    <asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
    
            <!-- Category selection -->
            <asp:DropDownList ID="categoryDropDownList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="categoryDropDownList_SelectedIndexChanged">
                <asp:ListItem Text="Please select a category" Value="0"></asp:ListItem>
            </asp:DropDownList>
    
            <br />
    
            <!-- Used to store the drop downs -->
            <asp:Panel ID="dropDownContainer" runat="server"></asp:Panel>
    
        </ContentTemplate>
    </asp:UpdatePanel>
    

    代码

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadDropDownLists();
    }
    
    private void LoadDropDownLists()
    {
        //Dummy data, you would pull your categories/attributes from whichever datasource
        //you are using
        var categories = new[]{
            new { Name = "Category 1", Id = 1, Attributes = new string[]{"GA", "FA", "RA"} },
            new { Name = "Category 2", Id = 2, Attributes = new string[]{"GA", "NA"} }
        };
    
        //Loop through the categories, load the dropdown
        foreach (var category in categories)
        {
            if (!IsPostBack)
                categoryDropDownList.Items.Add(new ListItem(category.Name, category.Id.ToString()));
    
            //For each attribute create a drop down and populate with data as required
            foreach (var attribute in category.Attributes)
            {
                string dropDownListId = FormatDropDownListId(attribute);
                if (!DropDownListExists(dropDownListId))
                {
                    DropDownList dropDownList = new DropDownList();
                    dropDownList.ID = dropDownListId;
                    dropDownList.Visible = false;
    
                    dropDownList.Items.Add(new ListItem(attribute));
    
                    dropDownContainer.Controls.Add(dropDownList);
                }
            }
        }
    }
    
    private bool DropDownListExists(string id)
    {
        DropDownList dropDownList = (DropDownList)dropDownContainer.FindControl(id);
        return dropDownList != null;
    }
    
    protected void categoryDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Reset all visible dropdowns
        HideAllDropDownLists();
    
        //Get the selected category
        string selectedItem = categoryDropDownList.SelectedItem.Value;
        switch (selectedItem)
        {
            case "1":
                {
                    //Here you would connect to db and pull correct attributes
                    //then set the visible dropdowns as required
                    SetDropDownVisibility(FormatDropDownListId("GA"));
                    SetDropDownVisibility(FormatDropDownListId("FA"));
                    SetDropDownVisibility(FormatDropDownListId("RA"));
                } break;
            case "2":
                {
                    SetDropDownVisibility(FormatDropDownListId("GA"));
                    SetDropDownVisibility(FormatDropDownListId("NA"));
                } break;
        }
    }
    
    private void SetDropDownVisibility(string id)
    {
        DropDownList dropDownList = (DropDownList)dropDownContainer.FindControl(id);
        if(dropDownList != null)
            dropDownList.Visible = true;
    }
    
    private void HideAllDropDownLists()
    {
        foreach (Control control in dropDownContainer.Controls)
        {
            control.Visible = false;
        }
    }
    
    private string FormatDropDownListId(string id)
    {
        return string.Format("dropDown{0}", id);
    }
    

    【讨论】:

      【解决方案3】:

      如果您使用的是 ASP.NET 网络表单,那么我认为您不需要使用 AJAX 或 JavaScript。

      我会做以下事情

      1. 在您的组合框上设置autopostback = true
      2. 为组合框的 OnChanged 事件添加事件处理程序
      3. 在事件处理程序中,应用规则来加载/生成/填充子组合框
      4. 将这些组合框添加到表单中

      您可以隐藏组合框(正如我在@jdavies 的回答中看到的那样),或者不使用任何组合框并动态创建并将它们添加到表单中。

      这个问题涉及同样的问题:

      DropDownList and Update Panel

      【讨论】:

        猜你喜欢
        • 2017-07-22
        • 2010-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-12
        • 2010-12-02
        • 1970-01-01
        • 2012-03-18
        相关资源
        最近更新 更多