Subclass web.ui.page

A

Arne Garvander

What is the proper way to subclass web.ui.page?
Public Class MyPage
Inherits System.Web.UI.Page

Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init
'MyBase.OnInit(e)
ViewStateUserKey = Session.SessionID
End Sub
End Class

When I called OnInit in my base class I got an infinite loop.
 
B

bruce barker

Page_Init is an event fired by OnInit, so when you call OnInt, it calls
Page_Init.
you only need to call base.OnInit if you override OnInit, not if you
register an event handler.

-- bruce (sqlwork.com)
 
A

Arne Garvander

Should I override oninit or handle init?
What would be the syntax to override oninit?
 
M

Milosz Skalecki [MCAD]

Howdy Arne,

Infinite loop is caused by calling OnInit from the event handler (C#):

protected virtual void OnInit(EventArgs e)
{
// this raises the vent you're handling through Page_Init
if (Init != null)
Init(this, e);
}

which is (simplifying) equivalent to:

protected virtual void OnInit(EventArgs e)
{
if (Init != null)
{
// equivalent to replacing Init with your event handler
OnInit(e);
}
}

As you can see, OnInit calls itself causing infinite loop and stack overflow.

You should change it to:
Public Class MyPage
Inherits System.Web.UI.Page

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)

MyBase.OnInit(e)
ViewStateUserKey = Session.SessionID

End Sub

End Class

There's one more thing: could you please explain why you're setting a view
state property on every postback?

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top