 |
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 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
|
|
|

September 5th, 2007, 01:48 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
quote:First, no, you can not have t[w]o page load events regardless of where they appear.
|
That's not true. You can have many Load handlers. Try this:
ASPX
Code:
<%@ Page Language="VB" ... AutoEventWireup="True"
CodeFile="Default.aspx.vb" ....%>
<script type="text/VB" runat="server">
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs)
Label1.Text += "From Load ASPX<br />"
End Sub
</script>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
followed by this code behind:
Code Behind
Code:
Protected Sub Page_Load1(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Label1.Text += "From Load 1<br />"
End Sub
Protected Sub Page_Load2(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Label1.Text += "From Load 2<br />"
End Sub
Protected Sub Page_Load3(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Label1.Text += "From Load 3<br />"
End Sub
If you run this, you'll see that all four methods get fired, albeit in a somewhat unpredictable order.
Now, whether this is a smart thing to do from a maintainability point of view is of course something completely different. I am sure that most people won't like this design.... ;)
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|

September 5th, 2007, 02:02 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I couldn't agree more. One handler for one event is usually more than enough. More handlers just make your code so much harder to read and understand. Additionally, since you can't really predict order and behavior, you may end up with subtle bugs....
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|

September 5th, 2007, 02:04 PM
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
dparsons-
Yes, CWebPage is a massive beast of a class that modifies System.Web.UI.Page
In my web.config, customErrors was set to On, but there was a page redirect attribute. Taking that out revealed that the actual error I am receiving is this:
The following error has occurred:
Class CustSignIn Error=Object reference not set to an instance of an object.
Although as far as I can tell, everything is declared explicitly now.
I think I'll give Imar's suggestion a try and see what happens...
|

September 5th, 2007, 02:08 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
If by "giv[ing] Imar's suggestion a try and see what happens..." you mean implementing two Page_Load handlers, you misunderstood me. I am not recommending to do that at all. I only showed that it was possible, not that it's a good or clean solution.
If you have the need for this, you may have other problems that you may need to fix instead....
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|

September 5th, 2007, 02:08 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
mmm.. make one more try.. surround your database code with a try catch to see if the error comes from there..
Maybe you are not opening the database or something like this???
if you comment that code the page works ok??
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|

September 5th, 2007, 02:34 PM
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for at least trying to help me, by the way. It is very much appreciated.
Yes, if I comment out those lines in the Codebehind and reinsert this little block back into the aspx file, it works perfectly:
Code:
<script type="text/vb" runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dbconn As New SqlConnection( "server=server.net;uid=username;pwd=password;database=test_storefrontdb" )
dbconn.Open()
Dim dbcomm As New SqlCommand( "SELECT * FROM Products", dbconn )
Dim dbread As SqlDataReader = dbcomm.ExecuteReader()
FeaturedItems.DataSource = dbread
FeaturedItems.DataBind()
dbread.Close()
dbconn.Close()
End Sub
</script>
The unfortunate problem with that is that then the login function which runs from the Runbehind's Page_Load() doesn't work anymore. I really only want to do two things on the page: Provide login functionality and pull up a list of items from a database. If I put the same block of code in the Codebehind, for some reason it just doesn't want to work. Am I missing something obvious? I didn't foresee this being such a complicated thing.
Thanks,
blu
|

September 5th, 2007, 02:38 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
can you try put it back in the code behind file and surround it with a try catch to see which error did it throw??
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|
|
 |