2013-03-04 44 views
2

我有一个.ascx文件,我可以在没有问题,使用Equivilent Loadcontrol

<uc1:EL ID="EL1" BusinessID="8" runat="server" /> 

凡BusinessID是公共财产加载。

Public Property BusinessID As Integer 
    Set(ByVal value As Integer) 
     _BusinessID = value 
    End Set 
    Get 
     Return _BusinessID 
    End Get 
End Property 

我可能需要多次将此ascx文件加载到具有不同BusinessID变量值的占位符中。

什么是等效的LoadControl方式?

回答

2

首先,您需要创建用户控件的实例,然后获取占位符的句柄。然后你可以添加一个到另一个。例如:

'get place holder 
Dim getPh As New PlaceHolder 
getPh = CType(Me.FindControl("myPlaceHolder"), PlaceHolder) 

'get user controls  
Dim newUserControl As New user_controls_myControlName 
newUserControl = CType(LoadControl("~/user_controls/myControlName.ascx"), user_controls_myControlName)  

getPh.Controls.Add(newPortlet) 

一旦创建了用户控件的实例,您可以访问其所有属性,包括BusinessID,你可以指定任何你想要的。

忘了一件事,你需要在你的ASCX文件的客户端代码添加一个引用,就像这样:

<%@ Reference Control="~/user_controls/myControlName.ascx"%> 
+0

感谢响应,但什么是user_control_myControlName? – 2013-03-04 19:08:17

+0

好的,我使用声明的变量将它到达页面。使用newUserControl.BusinessID =(数字),什么是正确的语法? – 2013-03-04 19:12:48

+0

user_control_myControlName是用户控制对象的名称。你应该在Intellisence中看到类似这种格式的东西,但用你的用户控件的名字。这个例子使用称为“myControlName”的用户控件。仅供参考,有时在添加新参考后,在您清理并重建项目之前,智能功能无法正常工作。 – jason 2013-03-04 19:17:36