如何在asp.net页面中添加动态数量的UserControls?

问题描述:

这里的问题:如何在asp.net页面中添加动态数量的UserControls?

我有一个(几乎)空的aspx页面,我想在此页面中插入一定数量的相同的用户控件。

事实是,我不知道他们会需要多少人。

我试图从CodeBehind中添加它们,但似乎UserControls是完全空的。

在主网页(MainDiv是RUNAT = “服务器” 一个div):

protected void Page_Init(object sender, EventArgs e) 
{ 
    WebUserControl1 uc = new WebUserControl1(); 
    WebUserControl1 uc1 = new WebUserControl1(); 
    MainDiv.Controls.Add(uc); 
    MainDiv.Controls.Add(uc1); 
} 

(不,如果我把这个代码放在Page_Init,Page_Load中或Page_PreRender工作)

用户控件(GW是包含在用户控件一个GridView):

protected void Page_PreRender(object sender, EventArgs e) 
{ 
    if (_data != null) 
    { 
     gw.DataSource = _data; 
     gw.DataBind(); 
    } 
} 

当我到达那里,毛重为空(this.Controls.Count为0)。

我该如何解决这个问题?

这就是repeater control的用途。

+0

这真的很简单:S 谢谢 – p4bl0 2010-02-25 11:29:17

+0

**快照!** :-P好帖子大卫 – Patrick 2010-02-25 11:58:08