【问题标题】:Equivilent Loadcontrol等效负载控制
【发布时间】:2013-03-04 17:34:11
【问题描述】:

我有一个 .ascx 文件,使用它可以毫无问题地加载

<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 方法是什么?

【问题讨论】:

    标签: vb.net ascx


    【解决方案1】:

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

     '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"%>
    

    【讨论】:

    • 感谢您的回复,但 user_control_myControlName 是什么?
    • 好的,我使用声明的变量将它放到页面上。使用 newUserControl.BusinessID = (number),正确的语法是什么?
    • user_control_myControlName 是用户控件对象的名称。您应该在 Intellisence 中看到类似这种格式的内容,但带有您的用户控件的名称。此示例使用名为“myControlName”的用户控件。仅供参考,有时,在添加新引用后,智能在您清理和重建项目之前无法正常工作。
    • 你的意思是? Portals_0_Open_BusinessDirectory_EnhancedListing
    • 完美,哥们!!那是没有意义的重建。非常感谢
    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 2022-11-30
    相关资源
    最近更新 更多