[英]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.