从用户控件访问父页面属性

时间:2022-10-19 22:18:52

Trying to access a property from the parent page on my user control.

尝试从我的用户控件上的父页面访问属性。

Here's the start of my default.asp codebehind:

这是我的default.asp代码隐藏的开始:

Partial Class _Default
     Inherits System.Web.UI.Page

     Private _selectedID As String = "74251BK3232"

     Public Property SelectedID() As String
          Get
               Return _selectedID 
          End Get
          Set(ByVal value As String)
               _selectedID = value
          End Set
     End Property

Here's the start of my user control codebehind:

这是我的用户控制代码隐藏的开始:

Partial Class ctrlAddAttribute
    Inherits System.Web.UI.UserControl
     Dim selectedID As String = Me.Parent.Page.selectedID()

I'm getting the error "selectedID is not a member of System.Web.UI.Page"

我收到错误“selectedID不是System.Web.UI.Page的成员”

Please adivse!

2 个解决方案

#1


9  

You could access the property when you cast the Page to your actual implementaion called _Default.

将页面强制转换为名为_Default的实际实现时,可以访问该属性。

Dim selectedID As String = DirectCast(Me.Page,_Default).selectedID()

But that is not the purpose of a Usercontrol(reusability). Normally you would give the ID from the Controller(page) to the UserControl.

但这不是Usercontrol(可重用性)的目的。通常,您可以将Controller(页面)中的ID提供给UserControl。

So define a property in the UserControl and set it from the page. On this way the UserControl would still work in other pages.

因此,在UserControl中定义属性并从页面设置它。通过这种方式,UserControl仍然可以在其他页面中使用。

#2


1  

Because the user control doesn't belong to the page, you can't access it directly unless you either explicitly set a property in the usercontrol from the including page or create a recursive function that cycles through all of the parent objects until it finds an object of type System.Web.UI.Page.

由于用户控件不属于该页面,因此除非您从包含页面在usercontrol中显式设置属性,否则无法直接访问它,或者创建循环遍历所有父对象的递归函数,直到找到System.Web.UI.Page类型的对象。

For the first, you could use a property (I've done this using a property called ParentForm) in the user control:

对于第一个,您可以在用户控件中使用属性(我使用名为ParentForm的属性完成此操作):

Private _parentForm as System.Web.UI.Page
Public Property ParentForm() As System.Web.UI.Page  ' or the type of your page baseclass
    Get
        Return _parentForm
    End Get
    Set
        _parentForm = value
    End Set
End Property

In the "Parent" page, you would set this property in an event as early as possible. I prefer to use PreLoad because it comes before the load (thus is available when most other controls would need it) and after the init.

在“父”页面中,您可以尽早在事件中设置此属性。我更喜欢使用PreLoad,因为它在加载之前(因此在大多数其他控件需要它时可用)和init之后。

Protected Sub Page_PreLoad(ByVal sender as Object, ByVal e as EventArgs) Handles Me.PreLoad
    Me.myUserControlID.ParentForm = Me
End Sub

You could also write a function trolls through the parent controls to find the page. Following code is untested so it may require tweaking, but the idea is sound.

您还可以通过父控件编写函数巨魔来查找页面。以下代码未经测试,因此可能需要调整,但这个想法是合理的。

Public Shared Function FindParentPage(ByRef ctrl As Object) As Page
    If "System.Web.UI.Page".Equals(ctrl.GetType().FullName, StringComparison.OrdinalIgnoreCase) Then
        Return ctrl
    Else
        Return FindParentPage(ctrl.Parent)
    End If
End Function

EDIT: You also can't access this property directly because it does not exist within the type System.Web.UI.Page. As @Tim Schmelter recommends, you can either attempt to cast the page to the specific page type _Default or if this is something common to many of your pages, you may need to create a base page class and include your property in that class. Then you can inherit this class instead of System.Web.UI.Page

编辑:您也无法直接访问此属性,因为它在System.Web.UI.Page类型中不存在。正如@Tim Schmelter建议的那样,您可以尝试将页面强制转换为特定页面类型_Default,或者如果这是您的许多页面的常见内容,则可能需要创建基页类并在该类中包含您的属性。然后,您可以继承此类而不是System.Web.UI.Page

Public Class MyBasePage
    Inherits System.Web.UI.Page

    Public Property SelectedID() as Integer
        ...
    End Property
End Class

Then in the page:

然后在页面中:

Partial Class _Default
    Inherits MyBasePage

    ...

End Class

#1


9  

You could access the property when you cast the Page to your actual implementaion called _Default.

将页面强制转换为名为_Default的实际实现时,可以访问该属性。

Dim selectedID As String = DirectCast(Me.Page,_Default).selectedID()

But that is not the purpose of a Usercontrol(reusability). Normally you would give the ID from the Controller(page) to the UserControl.

但这不是Usercontrol(可重用性)的目的。通常,您可以将Controller(页面)中的ID提供给UserControl。

So define a property in the UserControl and set it from the page. On this way the UserControl would still work in other pages.

因此,在UserControl中定义属性并从页面设置它。通过这种方式,UserControl仍然可以在其他页面中使用。

#2


1  

Because the user control doesn't belong to the page, you can't access it directly unless you either explicitly set a property in the usercontrol from the including page or create a recursive function that cycles through all of the parent objects until it finds an object of type System.Web.UI.Page.

由于用户控件不属于该页面,因此除非您从包含页面在usercontrol中显式设置属性,否则无法直接访问它,或者创建循环遍历所有父对象的递归函数,直到找到System.Web.UI.Page类型的对象。

For the first, you could use a property (I've done this using a property called ParentForm) in the user control:

对于第一个,您可以在用户控件中使用属性(我使用名为ParentForm的属性完成此操作):

Private _parentForm as System.Web.UI.Page
Public Property ParentForm() As System.Web.UI.Page  ' or the type of your page baseclass
    Get
        Return _parentForm
    End Get
    Set
        _parentForm = value
    End Set
End Property

In the "Parent" page, you would set this property in an event as early as possible. I prefer to use PreLoad because it comes before the load (thus is available when most other controls would need it) and after the init.

在“父”页面中,您可以尽早在事件中设置此属性。我更喜欢使用PreLoad,因为它在加载之前(因此在大多数其他控件需要它时可用)和init之后。

Protected Sub Page_PreLoad(ByVal sender as Object, ByVal e as EventArgs) Handles Me.PreLoad
    Me.myUserControlID.ParentForm = Me
End Sub

You could also write a function trolls through the parent controls to find the page. Following code is untested so it may require tweaking, but the idea is sound.

您还可以通过父控件编写函数巨魔来查找页面。以下代码未经测试,因此可能需要调整,但这个想法是合理的。

Public Shared Function FindParentPage(ByRef ctrl As Object) As Page
    If "System.Web.UI.Page".Equals(ctrl.GetType().FullName, StringComparison.OrdinalIgnoreCase) Then
        Return ctrl
    Else
        Return FindParentPage(ctrl.Parent)
    End If
End Function

EDIT: You also can't access this property directly because it does not exist within the type System.Web.UI.Page. As @Tim Schmelter recommends, you can either attempt to cast the page to the specific page type _Default or if this is something common to many of your pages, you may need to create a base page class and include your property in that class. Then you can inherit this class instead of System.Web.UI.Page

编辑:您也无法直接访问此属性,因为它在System.Web.UI.Page类型中不存在。正如@Tim Schmelter建议的那样,您可以尝试将页面强制转换为特定页面类型_Default,或者如果这是您的许多页面的常见内容,则可能需要创建基页类并在该类中包含您的属性。然后,您可以继承此类而不是System.Web.UI.Page

Public Class MyBasePage
    Inherits System.Web.UI.Page

    Public Property SelectedID() as Integer
        ...
    End Property
End Class

Then in the page:

然后在页面中:

Partial Class _Default
    Inherits MyBasePage

    ...

End Class