p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   General .NET (http://p2p.wrox.com/forumdisplay.php?f=122)
-   -   Page_Load not working! (http://p2p.wrox.com/showthread.php?t=16068)

New2ASPnet July 28th, 2004 08:50 AM

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.

planoie July 28th, 2004 09:36 AM

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.

New2ASPnet July 28th, 2004 10:01 AM

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?


New2ASPnet July 28th, 2004 10:08 AM

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.


Imar July 28th, 2004 11:22 AM

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.

planoie July 28th, 2004 11:52 AM

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.

New2ASPnet July 28th, 2004 01:53 PM

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!



All times are GMT -4. The time now is 11:38 AM.

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