【问题标题】:Asp.net Dynamically recreating ListBoxes on PostBackAsp.net 在 PostBack 上动态重新创建列表框
【发布时间】:2013-05-08 19:15:43
【问题描述】:

我想根据主要类别和子类别创建动态添加的ListBoxes,它们可以是无限数量的子类别。我整个星期都在研究这个问题,但无法弄清楚。

我希望它像 Ebay 类别选择一样。当页面加载时,会有一个包含所有主要类别的列表框。当用户在列表框中选择一个项目时,应动态添加另一个列表框,以便每个列表框都保留选定的项目。因此,在第一个列表框中选择第一个项目后,应该有两个列表框,第二个列表框显示属于所选列表的所有子类别。请看截图。

Ebay Category Selection Example http://www.aquariumbids.com/Images/ebayCat.JPG

每个请求的代码 - 我想它接近工作了。

    public partial class WebForm2 : System.Web.UI.Page
{
    private Int32 controlCount = 0;
    Panel _panel;

    private Panel PanelPlaceholder
    {
        get
        {
            if (_panel == null && Master != null)
                _panel = pnlContainer;
            return _panel;
        }
    }

    protected void Page_PreInit(Object sender, EventArgs e)
    {
        this.EnsureChildControls();

        if (IsPostBack)
        {
            // Re-create controls but not from datasource
            // The controlCount value is output in the page as a hidden field during PreRender.
            controlCount = Int32.Parse(Request.Form["controlCount"]); // assigns control count from persistence medium (hidden field)         
            for (Int32 i = 0; i < controlCount; i++)
            {
                CreateDynamicControlGroup(false);
            }
        }
    }
    protected void Page_Load(Object sender, EventArgs e)
    {
        // create from data query.
        // only if not postback
        if (!IsPostBack)
        {
            int cc = controlCount;

            DataTable dt = null;                
            Dictionary<string, string> Params = new Dictionary<string, string>();
            dt = Globals.g_DatabaseHandler.GetRecords(StoredProcedures.GetMainCategories, Params);

            // create a set of dynamic controls for the Row, incrementing counter and 
            // getting a reference to the new controls via their common parent (Dynamic PlaceHolder)
            CreateDynamicControlGroup(true);

            ListBox lb = (ListBox)PanelPlaceholder.Controls[controlCount - 1];

            // On reload you will see that child ListItems are persisted by the DropDownList
            lb.DataSource = dt; // use the same table
            lb.DataValueField = "ID";
            lb.DataTextField = "Name";
            lb.DataBind();
        }
    }


    protected void Page_PreRender(Object sender, EventArgs e)
    {
        // persist control count
        ClientScript.RegisterHiddenField("controlCount", controlCount.ToString());
    }


    private void ListBox_SelectedIndexChanged(Object sender, EventArgs e)
    {
        ListBox lb = sender as ListBox;


        Dictionary<string, string> Params = new Dictionary<string, string>();
        Params.Add("parentID", lb.SelectedValue);
        DataTable Categories = Globals.g_DatabaseHandler.GetRecords(StoredProcedures.GetChildCategories, Params);

        if (Categories.Rows.Count > 0)
        {
            // create a set of dynamic controls for the Row, incrementing counter and 
            // getting a reference to the new controls via their common parent (Dynamic PlaceHolder)
            CreateDynamicControlGroup(true);

            ListBox newLb = (ListBox)PanelPlaceholder.Controls[controlCount - 1];
            // On reload you will see that child ListItems are persisted by the DropDownList
            newLb.DataSource = Categories; // use the same table
            newLb.DataValueField = "ID";
            newLb.DataTextField = "Name";
            newLb.DataBind();
        }
    }


    private void CreateDynamicControlGroup(Boolean incrementCounter)
    {
        // Create one logical set of controls do not assign values!
        ListBox lb = new ListBox();
        lb.AutoPostBack = true;
        lb.CssClass = "panel";
        PanelPlaceholder.Controls.Add(lb);

        // wire event delegate
        lb.SelectedIndexChanged += new EventHandler(ListBox_SelectedIndexChanged);

        if (incrementCounter)
        {
            controlCount += 1;
        }
    }
}  

标记:

 <div class="Column12" id="Form_NewListing">
    <h2 class="h2row">Create Your Listing - Step 1 of 2)</h2>
    <h3 class="h3row">Select a category</h3>
    <div class="panel">
        <asp:Panel ID="pnlContainer" runat="server"></asp:Panel>        

    </div>
</div>

【问题讨论】:

  • 可能想发布一些代码。我现在没有看到任何东西的猜测是,您正在添加控件而不检查是否发生回发但没有代码......
  • 上面的代码几乎可以工作了。我看到的是,如果我在以前的 ListBox 上进行选择,那些子项不会删除或更新自己。
  • 我的意思是几乎可以工作,上面的代码会根据之前的列表框选择生成动态列表框
  • 好的,经过进一步测试,它可以部分工作。 ListBoxes 在回发时重新创建。我现在看到的问题是如果有 4 个列表框并且用户单击 ListbOx 3 中的一个新项目,则第 4 个应该将子类别重新呈现给其选定的父项。我希望我能正确地解释自己。
  • @MikeBrown 对此有何更新?看到这个问题的答案会很有趣,即使它现在已经 3 岁了。

标签: c# asp.net


【解决方案1】:

如果您想在没有回发的情况下执行此操作,这里有一篇文章可能会有所帮助: 使用 Microsoft ASP.NET AJAX 框架创建级联下拉列表的步骤 http://support.microsoft.com/kb/976156

【讨论】:

  • 我想要每个类别和所有子类别的列表框,如显示的图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 1970-01-01
  • 2012-11-14
相关资源
最近更新 更多