简体   繁体   English

如何在页面上动态管理UserControl…?

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

my scenerio is like this, 我的场景就是这样,

i have to make a admin page ( header section ) in which i have to select single or multiple User Control from my dropdownlist .... 我必须创建一个管理页面(标题部分),在其中我必须从下拉列表中选择单个或多个用户控件。

which will be added in the page dynamically .... 这将动态添加到页面中。

how should i do it ? 我该怎么办?

currently my idea is like this 目前我的想法是这样的

when a some one selects and add a usercontrol from the dropdownlist list , i will add usercontrols tags in a textarea and save it in db ... 当某个人从下拉列表中选择并添加一个usercontrol时,我将在textarea中添加usercontrols标签并将其保存在db中。

and when index pages of website is called then header section will be rendered from database and displayed .. 当网站的索引页面被调用时,标题部分将从数据库中渲染并显示..

but how should i manage control tag which should be place on the top of the page in index.aspx while rendering it ?? 但是我应该如何在呈现它的同时管理应该放置在index.aspx中页面顶部的控件标记?

please i know at some point it would be difficult to understand but i will try my best to reply if you have any query related to my question 请我知道在某个时候很难理解,但是如果您有任何与我的问题相关的查询,我会尽力答复

take care 照顾自己

If i get your question correctly there is no need to store tags or anything in databse. 如果我正确地收到了您的问题,则无需在数据库中存储标签或任何东西。 Just the name and path of control (remember User Controls can only be loaded from same project) that you wana load. 只是您要加载的控件的名称和路径(请记住用户控件只能从同一项目加载)。 Here is the code sample to load a user control dynamically. 这是动态加载用户控件的代码示例。

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

And in the code behing the important part is "this.LoadControl("~/WebUserControl2.ascx");" 在代码中,重要的部分是“ this.LoadControl(“〜/ WebUserControl2.ascx”);“ Look at this article for more info and loading user controls Dynamically creating User Controls 查看本文以获取更多信息并加载用户控件动态创建用户控件

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
        }

    }

Hope this helps, Thanks 希望这会有所帮助,谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM