首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用C#从服务器端加载具有用户控件的多个内容占位符

使用C#从服务器端加载具有用户控件的多个内容占位符
EN

Stack Overflow用户
提问于 2010-08-20 17:08:02
回答 1查看 1.5K关注 0票数 0

如何使用C#从服务器端加载带有用户控件的多个内容占位符。

目前,我在服务器端(在page_load中)加载一个用户控件,如下所示:

代码语言:javascript
复制
ContentPlaceHolder cph = this.Master.FindControl("TopContentPlaceHolder") as ContentPlaceHolder;
UserControl uc = this.LoadControl("~/webusercontrols/topmenu.ascx") as UserControl;
if ((cph != null) && (uc != null))
{
    cph.Controls.Add(uc);
}

我需要在我的页面中加载8个用户控件。我如何才能实现同样的目标?提前谢谢..

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-20 17:27:12

代码语言:javascript
复制
string[] listOfControls; //initialize as you will
ContentPlaceHolder cph = this.Master.FindControl("TopContentPlaceHolder") as ContentPlaceHolder;
for (int i=0; i<listOfControls.Length; i++)
{
   UserControl uc = this.LoadControl(listOfControls[i]) as UserControl;
   if ((cph != null) && (uc != null))
   {
       cph.Controls.Add(uc);
   }
}

或者,如果您希望每个占位符都有一个用户控件,您可以创建一个占位符列表,格式为vell

代码语言:javascript
复制
string[] placeholders;
string[] listOfControls; //initialize as you will

    for (int i=0; i<listOfControls.Length; i++)
    {
       ContentPlaceHolder cph = this.Master.FindControl(placeholders[i]) as ContentPlaceHolder;
       UserControl uc = this.LoadControl(listOfControls[i]) as UserControl;
       if ((cph != null) && (uc != null))
       {
           cph.Controls.Add(uc);
       }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3529521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档