【问题标题】:asp.net Recreating Dynamically Added ListBox controls on PostBackasp.net 在 PostBack 上重新创建动态添加的 ListBox 控件
【发布时间】:2013-05-08 21:42:55
【问题描述】:

所以我几乎有这个工作。我根据用户选择创建动态列表框。当用户第一次加载页面时,总是只有一个 ListBox 显示顶级类别(没有父类别的)。我有带有子类别的类别。可以有很多这样的子类别

猫 1

猫 2

  • 猫 2.1

  • 猫 2.2

    -- Cat 2.2.1
    
          --- Cat 2.2.1.1
    

等等。

如果用户从已显示的列表框中选择一个值,我遇到的问题是清除列表框。因此,如果显示了 4 个列表框,并且用户从第一个列表框中选择了一个新值,该值显示没有父级的顶部撕裂类别,则所有列表框都应该消失并且新的列表框应该出现。如果有 4 个 Listboxes 并且用户单击 ListbOx 3 中的新项目,则第 4 个应该将子类别重新呈现给其选定的父项。我希望我能正确地解释自己。

到目前为止,这是我的代码: 公共部分类 WebForm2:System.Web.UI.Page { 私有 Int32 controlCount = 0; 面板_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)
    {

        if (!IsPostBack)
        {
            int cc = controlCount;

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

            CreateDynamicControlGroup(true);

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

            lb.DataSource = dt;
            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)
        {
            CreateDynamicControlGroup(true);

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

            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;
        }
    }
}

这是我的标记:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<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>

提前致谢。

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    添加呢

    int index = PanelPlaceholder.Controls.IndexOf((ListBox)sender);
    for (int i = index + 1; i < PanelPlaceholder.Controls.Count; i++)
        PanelPlaceholder.Controls.RemoveAt(index + 1);
    

    到您的ListBox_SelectedIndexChanged 方法的开头?

    【讨论】:

    • 通过一些小的调整关闭它似乎正在工作。你的 sn-p 的这个问题是,如果存在其他具有更高索引的列表框,一旦你删除 at,就会发生索引超出范围。
    【解决方案2】:
    int index = PanelPlaceholder.Controls.IndexOf((ListBox)sender);
    for (int i = PanelPlaceholder.Controls.Count - 1; i > index; i--)
    {
      PanelPlaceholder.Controls.RemoveAt(i);
      controlCount--;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 2017-07-22
      • 2011-12-22
      相关资源
      最近更新 更多