2013-09-30 70 views
0

我有一個面板,我試圖從後面的代碼添加一個用戶控件。問題是一旦代碼運行,加載的控件不會出現在面板中。我錯過了什麼嗎?動態添加用戶控件到頁面

<div id="ShippingDetails" runat="server" visible="true"> 
     <asp:Panel ID="Panel1" runat="server"></asp:Panel> 
    </div> 
</form> 
</asp:Content> 

後面的代碼:

protected void Page_PreRender(object sender, EventArgs e) 
{ 
    if (OrderSearch1.SelectedValues != null) 
    { 
     InitializeShippingDetailControls(); 
    } 
} 

public void InitializeShippingDetailControls() 
{ 
    admin_InputControl control = new admin_InputControl(); 
    control.InitializeInputControl(ShippingDetailInputControls, new DataModels.EDIOrderShipmentInfo(), 1, "Shipment Details"); 
    Panel1.Controls.Add(control); 
} 

回答

0

顯然,你需要使用LoadControl,如果你正在處理一個真正的用戶控件。將我的代碼更改爲..

admin_InputControl control = (admin_InputControl)LoadControl("../controls/InputControl.ascx"); 
    control.InitializeInputControl(ShippingDetailInputControls, new DataModels.EDIOrderShipmentInfo(), 1, "Shipment Details"); 
    Panel1.Controls.Add(control);