2010-04-26 97 views
0

我有一個用戶控件,比如SearchVendor.ascx,它包含4個按鈕一個gridview和另一個用戶控件。 我需要動態實際加載控件作爲一個模式彈出動態添加用戶控件的事件處理

我得到這個代碼

var uc = Page.LoadControl("~/blah/VendorProductSearch.ascx") as VPSearch; 
uc.ShowVPSearch(true); 
_tempPlaceHolder.Controls.Add(uc); 

它工作正常控制才能正確但的onclick加載任何按鈕的控制消失,第二次? ?

我要動態添加的控制保留到取消按鈕

任何想法就如何實現這一目標用戶點擊?

感謝&問候, 弗朗西斯P.

+0

所以要清楚,控件是否在回傳後消失? – 2010-04-26 18:45:02

回答

0

你需要每一個回發後到控件添加到頁面。我正在處理類似於未知數量控件的事情,並且保留添加的數量,以便在回發時重新添加它們。要在取消時將其刪除,您需要調用Controls.Remove「myUserControlID」(傳入您分配的ID)。

public int AddressCount 
{ 
    get 
    { 
     string s = (string)ViewState["addressCount"]; 
     return ((s == null) ? 1 : Convert.ToInt32(s)); 
    } 

    private set { ViewState["addressCount"] = value.ToString(); } 
}  

protected void Page_Load(object sender, EventArgs e) 
{ 
    for (int i = 0; i < AddressCount; i++) 
    { 
     Control ai = LoadControl("~/controls/addressItem.ascx"); 
     addressContainer.Controls.Add(ai); // addressContainer is a PlaceHolder control on the page 
    } 
}