2010-02-25 47 views
2

這裏的問題:如何在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)。

我該如何解決這個問題?

回答

2

這就是repeater control的用途。

+0

這真的很簡單:S 謝謝 – p4bl0 2010-02-25 11:29:17

+0

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

相關問題