 |
| ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 24th, 2004, 02:34 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Overrinding PageLoad help!
Hi, I buy the ASP.Net WebSite Programming by WROX
In the book I follows the instruction to add a default page that i will inherit on all aspx page in my site. This is useful to make some confiurations and security features for all my pages in my site.
In the code I inherit like this:
Code:
Public Class EdtNews
Inherits Test.Web.DefaultPage
I override the init like this:
Code:
#Region " Override "
Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
InitializeComponent()
End Sub
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
AddHandler Me.Load, AddressOf PageLoad
End Sub
#End Region
I add an new page load sub with no handles on it because the code in InitializeComponent do the job for me (see above).
Code:
Private Sub PageLoad(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Do something
End Sub
QUESTION:
- Why if a made modification in the desing of my form, the form designer add automaticaly an "Handles MyBase.Load" at the and of my sub PageLoad and he remove the "AddHandler Me.Load, AddressOf PageLoad" in my InitializeComponent sub?
- How do I prevent this? When the form designer do this, I got an error at runtime and I need the re-write the right code!
Thanks for the help!
|
|

February 24th, 2004, 05:12 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
A) Why are you overriding page_init?
B) What is the runtime error you are getting?
|
|

February 24th, 2004, 05:35 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
All this stuff is for 2 things:
-to be able to add a custom error sub to all my page without the need to do it on all aspx page of my projet.
- To be able to modifie the IPrincipal objet for security. I nned to add more information than the username and is authentification state. I add email,adress,phone, etc...
Here is the defaultpage code:
Code:
Public Class DefaultPage
Inherits System.Web.UI.Page
' Page Events
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
AddHandler Me.Load, AddressOf BaultarPage_Load
AddHandler Me.Error, AddressOf BaultarPage_Error
End Sub
Private Sub DefaultPage_Load(ByVal sender As Object, ByVal e As EventArgs)
If Context.User.Identity.IsAuthenticated Then
If Not (TypeOf context.User Is SitePrincipal) Then
' ASP.NET's regular forms authentication picked up our cookie, but
' we() haven't replaced the default context user with our own.
' Let's do that now. We know that the previous context.user.
' identity.name is the login (because we forced it to be
' as such in the login page)
Dim newUser As New SitePrincipal(Context.User.Identity.Name)
Context.User = newUser
End If
End If
End Sub
Protected Sub BaultarPage_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim currentError As Exception = Server.GetLastError()
' Write error to log file if not already done by AppException
If Not (TypeOf currentError Is AppException) Then
AppException.LogError(currentError.Message.ToString)
End If
' Show error on screen
ShowError(currentError)
' Clear error so that it does not buble up to Application Level
Server.ClearError()
End Sub
I do not receive runtime error, all working. BUT when I come back to make a modification in the design (change color, etc) somethime (I don't know when and why) the form designer add an handle to the loadpage and remove the addhandler line in my oninit. When I do that I got an error because the programme do not go in the DefaultPage_Load sub of my defaultpage anymore. This cause an invalid cast because the right variabel assignement is in the defaultPage_Load sub.
I know, is not verry easy to understand, But I follows the procedure in my book. It work well, but I dont know why Visual Studio form designer change my code automaticaly!!
|
|

February 25th, 2004, 12:49 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Well, Visual Studio.Net likes to do a whole lot of things we'd prefer it not to do. Unfortunately I fear there isn't much anyone can do about that apart from not using it.
I understand what you are stating about the problem, but I'm not sure why you feel you need to override page_init instead of letting your DefaultPage methods use the standard "Handles <event>" syntax. I don't see why this would be a problem. The methods that you have written in the DefaultPage class will fire before those in a page class that you inherit from DefaultPage. But regardless of this, I can't see why the "Handles ..." syntax doesn't work in your inherited pages.
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

February 25th, 2004, 09:43 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I am not verry familiar with orverriding, but like you say, when I do the thing i do now, the load event is fire in my default page first. When VS change the handle thing, the load event is fired only in the base class and not in the default page. This cause a bug because the initialisation must be done in the befaultpage first.
Do you have an suggestion or an new approach to do that?
|
|

February 25th, 2004, 10:38 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
I've found that you should almost never use overriding. Unless you need to keep a parent class' method from doing what it's doing, you should avoid it. Stick with just using the page events.
Public Class DefaultPage
Inherits System.Web.UI.Page
Private Sub DefaultPage_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
...
End Sub
Protected Sub DefaultPage_Error(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Error
...
End Sub
End Class
DefaultPage.DefaultPage_Load() will execute before <inheritedpage>.Page_Load()
Peter
------------------------------------------------------
Work smarter, not harder.
|
|

September 7th, 2004, 10:20 PM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
What I seem to have found is that you cannot combine the two methods override or handles. This isn't obvious to most people, like myself.
I had taken the example from the book and tried to integrate it with one of my existing pages. In my parentpage class I was using overrides whilst in my child page I was using handles for the page load. Handles seems to fire first. This meant my parents page_load method fired after the child so the context.user did not exist. Frustrating.
However, if you use either both handles or both override, the code will work correctly (atleast it did in my quick test case).
This means that those of you who utilise the design method can use the handle method, providing that your parent page class implements the page_load as a handles too.
eg
'----------------------------------------
' This uses the handles method
'----------------------------------------
public class ourParentPage
inherits system.web.ui.page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not (TypeOf Context.User Is SitePrincipal) Then
Dim newUser As New SitePrincipal(Context.User.Identity.Name)
Context.User = newUser
End If
End Sub
end class
public class ourChildPage
inherits ourParentPage
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' rolesList is a datarepeater.... and the roles method is an arraylist
rolesList.DataSource = CType(Context.User, mynamespace.SitePrincipal).Roles
rolesList.DataBind()
End If
End Sub
end class
' The code above hooks the roles of my user into a data repeater and should be identical in functionality to the code below
'----------------------------------------
' This uses the overrides method
'----------------------------------------
public class ourParentPage
Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
' Note the handler is here
AddHandler Me.Load, AddressOf SitePage_Load
End Sub
Private Sub SitePage_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not (TypeOf Context.User Is SitePrincipal) Then
Dim newUser As New SitePrincipal(Context.User.Identity.Name)
Context.User = newUser
End If
End Sub
end class
public class ourChildPage
inherits ourParentPage
Private Sub InitializeComponent()
' Note the handler is here
AddHandler Me.Load, AddressOf Page_Load
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
' rolesList is a datarepeater.... and the roles method is an arraylist
rolesList.DataSource = CType(Context.User, mynamespace.SitePrincipal).Roles
rolesList.DataBind()
End If
End Sub
end class
'-------------------------------------
I hope this helps someone with the same problem I had for 6 hours... ;)
Cheers.
|
|
 |