簡體   English   中英

asp.net在PostBack上重新創建動態添加的ListBox控件

[英]asp.net Recreating Dynamically Added ListBox controls on PostBack

因此,我幾乎可以完成這項工作。 我根據用戶選擇創建動態列表框。 總是在用戶首次加載頁面時,只有一個顯示頂級類別的列表框(沒有父項的列表框)。 我有帶有子類別的類別。 可能是很多子類別

貓1

貓2

  • 貓2.1

  • 貓2.2

     -- Cat 2.2.1 --- Cat 2.2.1.1 

等等。

如果用戶從已經顯示的列表框中選擇一個值,則我正在清除列表框。 因此,如果顯示了4個列表框,並且用戶從第一個列表框中選擇了一個新值,該值顯示了沒有父項的最熱門的淚液類別,則所有列表框都應該消失,並且應該出現新的列表框。 如果有4個列表框,並且用戶在ListbOx 3中單擊一個新項目,則第4個應該將子類別重新呈現為其選定的父級。 我希望我能正確地解釋自己。

到目前為止,這是我的代碼:公共子類WebForm2:System.Web.UI.Page {private 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>

提前致謝。

怎么樣添加

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

到您的ListBox_SelectedIndexChanged方法的開頭?

int index = PanelPlaceholder.Controls.IndexOf((ListBox)sender);
for (int i = PanelPlaceholder.Controls.Count - 1; i > index; i--)
{
  PanelPlaceholder.Controls.RemoveAt(i);
  controlCount--;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM