locked
Massive Base Class RRS feed

  • Question

  • User357616875 posted

    I have a baseclass that deals with all kinds of requests and is the 404 errorpage, so can serve blogs, lists, business profiles etc etc.

    The baseclass is now to big, but can be divided up logically into sections in theory.

    About 50% of the base class in check values and adding user controls into place holders. It is fast and works well, but it is just too messy now to work with properly.

    Is there anyway I can create a new class inheriting the ui.page class and call it from the pageload and preinit events.

    I mean something like this

    Sub PageLoad

    Dim graham as new NewBaseClass
    Graham.pageloadstuff() 

    end Sub

    The problem seems to be when I have tryed it, that the pageloadstuff contains referance to me.master and master.findcontrol. I have done all the right imports and it does not seem to be able to be done

    If this is not possible, and I guess it is probally now how can I move sections of my baseclass to new classes and create a more logical code stucture. 






    Monday, April 12, 2010 6:01 PM

Answers

  • User-952121411 posted

    Yes you can do this.  You can either just use a plain class file or you can use a page that acts as your new Base page, but the actual page itself is never displayed; it is just used to create your new base class.

    You don't even have to have the page's manually call this new class.  If the existing page's inherit from this new class, and the new class inherits from System.Web.UI.Page, then all you need to do is Override the appropriate method (i.e. 'OnInit() ).  In this manner when all page's load, they will automatically call the overriden event in the new base class.  Take a look below:

    Public Class MyUIBaseClass
        Inherits System.Web.UI.Page
    
        Protected Overrides Sub OnInit(ByVal e As EventArgs)
    
            'Since this is a base class that is inherited by other pages
            'it will be called upon opening by any page that inherits this page.
    
            'Sample code implemented by inherited classes
            'Maintain the browser's scroll bar positions on the client after postbacks.
            Me.Page.MaintainScrollPositionOnPostBack = True
    
        End Sub
    
    End Class


    ...and here would be the sample code from a Default.aspx that inherits the new class.  Notice if you debug the code, it will 1st step into the 'OnInit' code in the inherited class:

    Public Partial Class _Default
        Inherits MyUIBaseClass
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
               '...
            End If
    
        End Sub
    
    End Class


    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 13, 2010 9:23 AM

All replies

  • User1190007792 posted

    I'm not a VB programmer but in C# partial classes might help in this kind of situations. You can split up your code in more or more code files to make it more readable.


    http://msdn.microsoft.com/en-us/library/wa80x488.aspx

    Tuesday, April 13, 2010 6:54 AM
  • User-952121411 posted

    Yes you can do this.  You can either just use a plain class file or you can use a page that acts as your new Base page, but the actual page itself is never displayed; it is just used to create your new base class.

    You don't even have to have the page's manually call this new class.  If the existing page's inherit from this new class, and the new class inherits from System.Web.UI.Page, then all you need to do is Override the appropriate method (i.e. 'OnInit() ).  In this manner when all page's load, they will automatically call the overriden event in the new base class.  Take a look below:

    Public Class MyUIBaseClass
        Inherits System.Web.UI.Page
    
        Protected Overrides Sub OnInit(ByVal e As EventArgs)
    
            'Since this is a base class that is inherited by other pages
            'it will be called upon opening by any page that inherits this page.
    
            'Sample code implemented by inherited classes
            'Maintain the browser's scroll bar positions on the client after postbacks.
            Me.Page.MaintainScrollPositionOnPostBack = True
    
        End Sub
    
    End Class


    ...and here would be the sample code from a Default.aspx that inherits the new class.  Notice if you debug the code, it will 1st step into the 'OnInit' code in the inherited class:

    Public Partial Class _Default
        Inherits MyUIBaseClass
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
               '...
            End If
    
        End Sub
    
    End Class


    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 13, 2010 9:23 AM