• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 809
  • Last Modified:

Retrieve values from Custom Base Page Class through a User Control

I am building an ASP.NET application that uses a custom BasePage Class for all .aspx pages.  Every time the page is loaded, it retrieves an object from Session State and stores it in a private variable.  It has a Public Property that allows the controls to access this object.

Public MustInherit Class BasePage
        Inherits Page

        Private objUser As CustomObject

        Protected Overrides Sub OnLoad(ByVal e As EventArgs)
            'Try to return the CustomObject from the Session
            Try
                objUser = CType(Session("CustomObject"), CustomObject)
            Catch
                objUser = New CustomObject()
            End Try

            ' Call the base OnLoad method
            MyBase.OnLoad(e)
        End Sub

        Public ReadOnly Property CustomObject() As CustomObject
            Get
                Return CustomObject
            End Get
        End Property

    End Class

When trying to access the public property from a UserControl, I get an object reference error because the User control cannot retrieve the information from the BasePage object because it doesn't know it exists as a BasePage, only as a Page object.  

If CType(Page, BasePage).CustomObject.CustomObjectProperty Then
     'Do something
End If

How can I create a custom BaseUserControl class that can reference the BasePage class reference the CustomObject Property?  

0
Underwhelmed
Asked:
Underwhelmed
  • 3
  • 3
1 Solution
 
w_shailaCommented:
I can suggest you an alternative way for your problem.

I am giving some sample here.

You have page (Hello.aspx) which is inherting from the Base Page and you have the User control on that page right. Insted of trying to access the base page in the user control , why can't u do like this.

1) In the User control expose events.
2) and in the Base page just use the UserControl variable , and write the implementation for those events which u defined in the User control.
3) finally Place the User control in the page.

It works . I did ths way and got the desired functioanlity.
if you are confused and have any concerns regarding this post me back.
0
 
ihenryCommented:
Your code almost looks correct, except in the OnLoad method. if the Session("CustomObject") variable never get initialize anywhere else, every page which inherits from BasePage class will be holding a null CustomObject property variable. And you'll get null reference exception whenever you're trying to get the CustomObject instance.
0
 
UnderwhelmedAuthor Commented:
To get this to work, would I move the OnLoad code into the OnInit Function?  After getting the object from the session and loading in into the BasePage class, to access it from a usercontrol, would I have to make sure that the inheritance is of type BasePage before retrieving the information of it?

If TypeOf Me.Page Is BasePage Then
    _objCust = CType(Me.Page, BasePage).CustomObject.CustomObjectProperty
End If

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ihenryCommented:
Nope. Normally, OnLoad method is triggered before page_load of the user control. So the code above should works. But where do you initialize the Session("CustomObject") variable?
0
 
UnderwhelmedAuthor Commented:
This is initialized in Global.asax in Session_Start and stored into the user's session.

 'Retrieve a new CustomObject and save it in the session
    HttpContext.Current.Session("CustomObject") = New CustomObject()

In the custom object constructor, everything is initialized with default values before it is populated depending on the user.


PS: I am able to retrieve information from the BasePage if I put my code in the aspx, but not if it's put into a usercontrol, which inherits from UserControl.  So the problem lies in the retrieval of the BasePage object from the UserControl.

0
 
ihenryCommented:
Any problem if you change the code like this?

Public ReadOnly Property CustomObject() As CustomObject
      Get
            If Session("CustomObject") Is Nothing Then
                  Session("CustomObject") = New CustomObject()
            Else
                  Return CType(Session("CustomObject"), CustomObject)
            End If
      End Get
End Property
0
 
UnderwhelmedAuthor Commented:
Thanks for your help ihenry.  I got it to work using this code:

If TypeOf Me.Page Is BasePage Then
    _objCust = CType(Me.Page, BasePage).CustomObject.CustomObjectProperty
End If

Then changing the property in the BasePage to this code:

Public ReadOnly Property CustomObject() As CustomObject
     Get
          If Session("CustomObject") Is Nothing Then
               Session("CustomObject") = New CustomObject()
          Else
               Return CType(Session("CustomObject"), CustomObject)
          End If
     End Get
End Property

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now