1

我的场景是这样的

我必须制作一个管理页面(标题部分),我必须在其中从我的下拉列表中选择单个或多个用户控件....

这将在页面中动态添加....

我该怎么做?

目前我的想法是这样的

当有人从下拉列表中选择并添加用户控件时,我将在文本区域中添加用户控件标签并将其保存在数据库中...

并且当调用网站的索引页面时,标题部分将从数据库中呈现并显示..

但是我应该如何管理应该放置在 index.aspx 页面顶部的控件标记,同时呈现它?

请我知道在某些时候这会很难理解,但如果您有任何与我的问题相关的疑问,我会尽力回复

小心

4

1 回答 1

1

如果我正确地回答了您的问题,则无需在数据库中存储标签或任何内容。只是您想要加载的控件的名称和路径(记住用户控件只能从同一个项目加载)。这是动态加载用户控件的代码示例。

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

    }

希望这会有所帮助,谢谢

于 2012-01-01T07:42:11.023 回答