Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Parent Page Property from User Control

Tags:

asp.net

vb.net

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

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

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"

Please adivse!

like image 443
s15199d Avatar asked Nov 04 '10 14:11

s15199d


2 Answers

You could access the property when you cast the Page to your actual implementaion called _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.

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

like image 93
Tim Schmelter Avatar answered Oct 04 '22 07:10

Tim Schmelter


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.

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

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.

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

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
like image 35
Joel Etherton Avatar answered Oct 04 '22 06:10

Joel Etherton