Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 28th, 2004, 08:50 AM
Registered User
 
Join Date: Jul 2004
Location: LaFox, IL, USA.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Page_Load not working!

This is just the dumbest question with probably a very simple answer. I am teaching myself ASP.Net from a book and the code examples all work except for when they use a Page_Load subroutine. Then nothing executes within this routine. Now this doesn't make any sense to me because it has to execute a page load even if the page loads, right? So why isn't it executing this very simple subroutine (see below)? Any insights? suggestions? I am stumped and don't know enough about ASP.NET to debug anything yet.

Code:

<%@ Page CodeBehind="CodeRender.aspx.vb" Language="vb" AutoEventWireup="false" Inherits="LearningSite.CodeRender" %>
<Script Runat="Server">
  Dim strSomeText As String
  Sub Page_Load
    strSomeText = "Hello!"
  End Sub
</Script>

<html>
<head><title></title></head>
<body>

<form Runat="Server">
<p>The value of strSomeText is: <%=strSomeText%></p>
<% strSomeText = "Goodbye!" %>
<P>The value of strSomeText is: <%=strSomeText%></p>
</form>

</body>
</html>

Simple, huh? Ugh. Microsoft. I'm sure it's their fault somehow.
Reply With Quote
  #2 (permalink)  
Old July 28th, 2004, 09:36 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The problem lies here:

AutoEventWireup="false"

This instructs the page to not automatically wire up a predefined list of function names to the page's events. The "Page_Load" function will normally be automatically wired up to the page's .Load event, but setting this attribute to false disables it. Set it to true and you'll find it works.
Reply With Quote
  #3 (permalink)  
Old July 28th, 2004, 10:01 AM
Registered User
 
Join Date: Jul 2004
Location: LaFox, IL, USA.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried changing that parameter like you said (it defaults like that in Visual Studio.NET but it still isn't executing anything in my Page Load subroutine. Is it maybe in the wrong place? Does it matter if the subroutine is located within the page or should it be in the code behind page? Would that make a difference?

Reply With Quote
  #4 (permalink)  
Old July 28th, 2004, 10:08 AM
Registered User
 
Join Date: Jul 2004
Location: LaFox, IL, USA.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OH! Now here's something interesting. When I changed AutoEventWireup="true" and then deleted Inherits="LearningSite.CopyField" from the Page directive, then it suddenly worked. Why would deleting the Inherits directive have anything to do with the Page_Load event? Is this going to affect anything adversely now that it's gone?

Sorry there's so many questions, but I'm really very new at this. It's so different from the regular ASP that I'm used to.

Reply With Quote
  #5 (permalink)  
Old July 28th, 2004, 11:22 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You're mixing up two technologies.

When you are using Visual Studio.NET, all code goes in a Code Behind file (named after your ASPX page with a VB or CS extension).

When the project is compiled, VS.NET takes this code and compiles all your pages in a single assembly. At run time, this single assembly is used to "map" your ASPX page to the class it derives from, and then the code in that class is executed. So, in VS.NET, the Page_Load is compiled into a class called MyPage for example, and when you request MyPage.aspx, the method MyPage.Page_Load will fire.

In "flat" ASPX pages, you have in-line code, usually located at the top of the page. You can put code there that is run when the page is requested.

When you mix these concepts, the runtime gets confused. Should it get the methods from the in-line code? or from the compiled assembly?

When you removed Inherits, you effectively detached the page from the assembly that VS.NET has created, making it a "stand alone" page.

If you are working with VS.NET, you're better off not writing in-line code. Instead, add all the code in the Code Behind file.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #6 (permalink)  
Old July 28th, 2004, 11:52 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Thank you for clarifying Imar.

Also: I realize that you are just learning now, but one piece of advice I must dispense is that you should avoid the classic ASP style <% %> syntax. This can cause problems and it degrades performance. Learn to do what you need in the code-behind. You will eventually see pieces of syntax with <% %>, but they will most likely be databinding syntax, which isn't quite the same as what your example is doing.
Reply With Quote
  #7 (permalink)  
Old July 28th, 2004, 01:53 PM
Registered User
 
Join Date: Jul 2004
Location: LaFox, IL, USA.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you everyone for your input. The help is much appreciated. Maybe someday when I get a little experience under my belt, I'll be able to contribute too!

Thanks again!

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page_Load question wayneamurphy BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 4 November 6th, 2006 03:54 AM
page_load() hassel vijayapteET ASP.NET 1.0 and 1.1 Basics 2 October 6th, 2005 01:30 PM
forcing a page_load ? Rashe ASP.NET 1.0 and 1.1 Basics 2 February 16th, 2004 10:53 AM
force Page_Load miguel.ossa ASP.NET 1.0 and 1.1 Basics 7 January 23rd, 2004 06:04 AM



All times are GMT -4. The time now is 05:47 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.