Using inheritance for web pages - why have a Page_Init when you can override OnInit?

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Using inheritance for web pages - why have a Page_Init when you can override OnInit?

    I'm trying to better understand the use of inheritance vs. the
    implementation of a handler. I wanted to ask this question to the newsgroup.

    Each aspx page is a class which Inherits System.Web.UI.Page.
    Then, each page has it's own implementation of Private Sub Page_Load and
    Private Sub Page_Init to handle the Handles MyBase.Load and Handles
    MyBase.Init events (respectively). The programmer (you or me) is supposed to
    flesh out the Pgae_Load and, if you are using the Visual Studio designer,
    not touch the Page_Init event handlers.

    Instead of this, why don't these page classes, which are subclasses of
    System.Web.UI.Page just overload the superclasses of the OnInit and OnLoad
    classes? That would seem to make more sense from a class hierarchy
    standpoint. Each page class, for example

    'Instead of this:

    Public Class WebForm1

    Inherits System.Web.UI.Page

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    'Put user code to initialize the page here

    End Sub

    End Class

    'Do this:

    Public Class WebForm1

    Inherits System.Web.UI.Page

    Protected Overrides Sub OnInit(ByVal e As EventArgs)

    'Put user code to initialize the page here

    MyBase.OnLoad(e)

    End Sub 'OnInit

    End Class








    Guest

  2. Similar Questions and Discussions

    1. page_init executing twice
      I am experiencing a weird problem with some buttons on my webpage. The page_init is executing twice when a button is clicked. If I replace the...
    2. Init Handler Not Firing After Page_Init
      Hello to all of you geniuses, I'm having a problem trying to get an Init handler to fire for a Placeholder control at the initialization phase....
    3. Online cannot come up oninit results
      16:55:29 Rowid 0xd4e702 not found for delete in partnum d00054,3 treep 0000000223f4ffb0: 40e0f902 02000000 37d20000 1cfe46fc @..........
    4. difference between constructor and Page_Init()
      Hi, what is the difference between the page-constructor and Page_Init() ? when to put what code where ? thanks chris
    5. Page_Init() and Page_Load()
      Hi, what is the purpose of having a Page_Init() AND a Page_Load() event handler 'cause both will always be executed on loading a page ? What...
  3. #2

    Default Using inheritance for web pages - why have a Page_Init when you can override OnInit?

    Sorry, I mixed up the code- reposting with correct example now :)

    I'm trying to better understand the use of inheritance vs. the
    implementation of a handler. I wanted to ask this question to the newsgroup.

    Each aspx page is a class which Inherits System.Web.UI.Page. Then, each page
    has it's own implementation of Private Sub Page_Load and Private Sub
    Page_Init to handle the Handles MyBase.Load and Handles MyBase.Init events
    (respectively). The programmer (you or me) is supposed to flesh out the
    Pgae_Load and, if you are using the Visual Studio designer, not touch the
    Page_Init event handlers.

    Instead of this, why don't these page classes, which are subclasses of
    System.Web.UI.Page just overload the superclasses of the OnInit and OnLoad
    classes? That would seem to make more sense from a class hierarchy
    standpoint. Each page class, for example

    'Instead of this:

    Public Class WebForm1

    Inherits System.Web.UI.Page

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    'Put user code to initialize the page here

    End Sub

    End Class

    'Do this:

    Public Class WebForm1

    Inherits System.Web.UI.Page

    Protected Overrides Sub OnLoad(ByVal e As EventArgs)

    'Put user code to initialize the page here

    MyBase.OnLoad(e)

    End Sub 'OnInit

    End Class



    Guest

  4. #3

    Default Re: Using inheritance for web pages - why have a Page_Init when you can override OnInit?

    Totally agree. So the VB.NET code generation does it as well as the C#.
    I get rid of them and override the base classes method as I would in any
    other class.
    You don't see people adding a handler for OnPreRender, you override don't
    you.
    I think who ever was writing the code generator was just being silly.

    Ben W

    PS Remember to call the base though



    <DanR@REMOVETHISTOGETTOME-warshawgroup.com> wrote in message
    news:OhhgCzlRDHA.2152@TK2MSFTNGP12.phx.gbl...
    > Sorry, I mixed up the code- reposting with correct example now :)
    >
    > I'm trying to better understand the use of inheritance vs. the
    > implementation of a handler. I wanted to ask this question to the
    newsgroup.
    >
    > Each aspx page is a class which Inherits System.Web.UI.Page. Then, each
    page
    > has it's own implementation of Private Sub Page_Load and Private Sub
    > Page_Init to handle the Handles MyBase.Load and Handles MyBase.Init
    events
    > (respectively). The programmer (you or me) is supposed to flesh out the
    > Pgae_Load and, if you are using the Visual Studio designer, not touch the
    > Page_Init event handlers.
    >
    > Instead of this, why don't these page classes, which are subclasses of
    > System.Web.UI.Page just overload the superclasses of the OnInit and OnLoad
    > classes? That would seem to make more sense from a class hierarchy
    > standpoint. Each page class, for example
    >
    > 'Instead of this:
    >
    > Public Class WebForm1
    >
    > Inherits System.Web.UI.Page
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    >
    > 'Put user code to initialize the page here
    >
    > End Sub
    >
    > End Class
    >
    > 'Do this:
    >
    > Public Class WebForm1
    >
    > Inherits System.Web.UI.Page
    >
    > Protected Overrides Sub OnLoad(ByVal e As EventArgs)
    >
    > 'Put user code to initialize the page here
    >
    > MyBase.OnLoad(e)
    >
    > End Sub 'OnInit
    >
    > End Class
    >
    >
    >

    Ben Guest

  5. #4

    Default Re: Using inheritance for web pages - why have a Page_Init when you can override OnInit?

    Disagree,

    As the author of the class you should just sequence the code correctly on
    the overridden OnLoad method!

    It is actually going against MSs own guidelines to attach an event handler
    for the same class that is raising the event.

    Ben

    "Natty Gur" <natty@dao2com.com> wrote in message
    news:uK33iNrRDHA.2460@TK2MSFTNGP10.phx.gbl...
    > Hi,
    >
    > You can override Onload function but OnLoad() is call by the framework
    > every time the page is loaded. But before the Page_Load() method is
    > called in your code-behind page. This allows you to do some action
    > before Page_load runs and the programmer code in the page_load executes,
    > such as security checking.
    >
    >
    > Natty Gur, CTO
    > Dao2Com Ltd.
    > 28th Baruch Hirsch st. Bnei-Brak
    > Israel , 51114
    >
    > Phone Numbers:
    > Office: +972-(0)3-5786668
    > Fax: +972-(0)3-5703475
    > Mobile: +972-(0)58-888377
    >
    > Know the overall picture
    >
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    Ben Guest

  6. #5

    Default Re: Using inheritance for web pages - why have a Page_Init when you can override OnInit?

    but, he is the user...

    Natty Gur, CTO
    Dao2Com Ltd.
    28th Baruch Hirsch st. Bnei-Brak
    Israel , 51114

    Phone Numbers:
    Office: +972-(0)3-5786668
    Fax: +972-(0)3-5703475
    Mobile: +972-(0)58-888377

    Know the overall picture


    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Natty Gur Guest

  7. #6

    Default Re: Using inheritance for web pages - why have a Page_Init when you can override OnInit?

    Hey, if you see this, drop me an email.
    I think you will figure out how.
    -Rz
    I like your return address. LOL


    "Ben" <mustbejoking@120spamsaday.con> wrote in message
    news:uFbIw4mRDHA.1896@TK2MSFTNGP12.phx.gbl...
    > Totally agree. So the VB.NET code generation does it as well as the C#.
    > I get rid of them and override the base classes method as I would in any
    > other class.
    > You don't see people adding a handler for OnPreRender, you override don't
    > you.
    > I think who ever was writing the code generator was just being silly.
    >
    > Ben W
    >
    > PS Remember to call the base though
    >
    >
    >
    > <DanR@REMOVETHISTOGETTOME-warshawgroup.com> wrote in message
    > news:OhhgCzlRDHA.2152@TK2MSFTNGP12.phx.gbl...
    > > Sorry, I mixed up the code- reposting with correct example now :)
    > >
    > > I'm trying to better understand the use of inheritance vs. the
    > > implementation of a handler. I wanted to ask this question to the
    > newsgroup.
    > >
    > > Each aspx page is a class which Inherits System.Web.UI.Page. Then, each
    > page
    > > has it's own implementation of Private Sub Page_Load and Private Sub
    > > Page_Init to handle the Handles MyBase.Load and Handles MyBase.Init
    > events
    > > (respectively). The programmer (you or me) is supposed to flesh out the
    > > Pgae_Load and, if you are using the Visual Studio designer, not touch
    the
    > > Page_Init event handlers.
    > >
    > > Instead of this, why don't these page classes, which are subclasses of
    > > System.Web.UI.Page just overload the superclasses of the OnInit and
    OnLoad
    > > classes? That would seem to make more sense from a class hierarchy
    > > standpoint. Each page class, for example
    > >
    > > 'Instead of this:
    > >
    > > Public Class WebForm1
    > >
    > > Inherits System.Web.UI.Page
    > >
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > >
    > > 'Put user code to initialize the page here
    > >
    > > End Sub
    > >
    > > End Class
    > >
    > > 'Do this:
    > >
    > > Public Class WebForm1
    > >
    > > Inherits System.Web.UI.Page
    > >
    > > Protected Overrides Sub OnLoad(ByVal e As EventArgs)
    > >
    > > 'Put user code to initialize the page here
    > >
    > > MyBase.OnLoad(e)
    > >
    > > End Sub 'OnInit
    > >
    > > End Class
    > >
    > >
    > >
    >
    >

    Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139