簡體   English   中英

如何在頁面上動態管理UserControl…?

[英]how to Manage UserControl dynamically on a page…?

我的場景就是這樣,

我必須創建一個管理頁面(標題部分),在其中我必須從下拉列表中選擇單個或多個用戶控件。

這將動態添加到頁面中。

我該怎么辦?

目前我的想法是這樣的

當某個人從下拉列表中選擇並添加一個usercontrol時,我將在textarea中添加usercontrols標簽並將其保存在db中。

當網站的索引頁面被調用時,標題部分將從數據庫中渲染並顯示..

但是我應該如何在呈現它的同時管理應該放置在index.aspx中頁面頂部的控件標記?

請我知道在某個時候很難理解,但是如果您有任何與我的問題相關的查詢,我會盡力答復

照顧自己

如果我正確地收到了您的問題,則無需在數據庫中存儲標簽或任何東西。 只是您要加載的控件的名稱和路徑(請記住用戶控件只能從同一項目加載)。 這是動態加載用戶控件的代碼示例。

  <asp:DropDownList ID="userControlSelection" runat="server" AutoPostBack="true"
    onselectedindexchanged="userControlSelection_SelectedIndexChanged">
      <asp:ListItem Value="1">User Control One</asp:ListItem>
      <asp:ListItem Value="2">User Control Two</asp:ListItem>
</asp:DropDownList>
<asp:Panel ID="controlHolder" runat="server" ></asp:Panel>

在代碼中,重要的部分是“ this.LoadControl(“〜/ WebUserControl2.ascx”);“ 查看本文以獲取更多信息並加載用戶控件動態創建用戶控件

protected void userControlSelection_SelectedIndexChanged(object sender, EventArgs e)
    {
        Control c = null;
        if (userControlSelection.SelectedValue == "1")
        {
            c = this.LoadControl("~/WebUserControl1.ascx");
        }
        else if (userControlSelection.SelectedValue == "2")
        {
            c = this.LoadControl("~/WebUserControl2.ascx");                
        }

        if (c != null)
        {
            controlHolder.Controls.Clear();
            controlHolder.Controls.Add(c);
        }
        else
        {
            //Throw some error
        }

    }

希望這會有所幫助,謝謝

暫無
暫無

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

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