Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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
 
Old June 14th, 2004, 01:30 PM
Authorized User
 
Join Date: Oct 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK here is my actual 'code behind' code

'Imports System.DateTime
Public Class WebForm1
    Inherits System.Web.UI.Page


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents time As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Sub Page_Load()
        'time.text = Hour(Now) & ":" & Minute(Now) & Second(Now)
    End Sub

    Private Sub time_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'time.Text = Hour(Now) & ":" & Minute(Now) & Second(Now)

    End Sub

    Private Sub btnHello_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'time.Text = Hour(Now) & ":" & Minute(Now) & Second(Now)
    End Sub
End Class

Now here is my html:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="punctual.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    <HEAD>
        <title>WebForm1</title>
        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
        <meta content="Visual Basic" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

This is all of the code I believe you would need to look at.

    </HEAD>
    <body bgColor="#ffffff" MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:TextBox id="time" style="Z-INDEX: 101; LEFT: 264px; POSITION: absolute; TOP: 16px" runat="server"></asp:TextBox>
            In WebServerLand the time is currently:
        </form>
    </body>
</HTML>


 
Old June 14th, 2004, 01:37 PM
Authorized User
 
Join Date: Oct 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My basic question is:
Shouldn't the code within the page_load sub run automatically ?
Why isn't it ?
I believe it MUST be some configuration error in my HTML. But what is the source of my error ?

 
Old June 14th, 2004, 01:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

1. Yes- Page_Load runs automagically.
2. Why do you have the ' (comment) symbol on the code? It won't run if it's commented out.





Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old June 14th, 2004, 02:07 PM
Authorized User
 
Join Date: Oct 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The comment was put in after the code did not run as expected.
I simply forgot to remove the comment before I sent you the code.
Can you attempt to run the example on your machine ?
It's such a short code example.
If you're using Visual Studio as I am, most of the code is pre-created for you. You simply need to add two lines of code.
According to the book anyway.

 
Old June 14th, 2004, 02:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

ok-

Visual Studio is not Web Matrix.. So, with VS.NET your creating code-behind pages. Web Matrix is just inline code. This is important as Code-behind creates a DLL that must be "hooked in".

If you look at this code:
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        time.Text = Hour(Now) & ":" & Minute(Now) & ":" & Second(Now)
    End Sub
You notice that the sub has a "HANDLES" clause. This tells the dll that it needs to listen for the event taking place. If you don't have that, the routine in code-behind never fires.

Web matrix puts it's code in the ASPX file itself. In this case, when it's not code behind, the handles routine is not required. In this way it behaves more like Classic ASP.

For example you could write, in VS.NET:
Code:
    Private Sub Nonstandard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        time.Text = Hour(Now) & ":" & Minute(Now) & ":" & Second(Now)
    End Sub
and it would work just fine. The Handles clause tells it to run on load.

VS.NET and Web Matrix are not the same. Unfortunately I don't think the book goes into enough depth of the differences.. and it's the problem your running into.


Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old June 14th, 2004, 02:26 PM
Authorized User
 
Join Date: Oct 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can you pls explain what actually happens when "HANDLES MYBASE.LOAD"
is encountered ?

Thank-you so much HAL .
I hate unresolved issues.

 
Old June 14th, 2004, 02:30 PM
Authorized User
 
Join Date: Oct 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Never mind the reply Hal I think I've got it .Thank-you BIG time.
Now I can move on.

 
Old June 14th, 2004, 02:54 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Even though you said you got it.. I want to put this here for others..

MYBASE is the generic name for the base class. (It's one of those cool classes like MY. When the Base Class (the page) loads - a load event is fired - since the Page class has that specified to fire.

the HANDLES MYBASE.LOAD tells the event proccessor that when it gets a LOAD event from the baseclass it should call the rouine specified.


Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old June 14th, 2004, 03:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

It is declaring that the Page_Load() method (in code) handles the Load event when the Page class raises the Load event.

Brian
 
Old June 16th, 2004, 09:43 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Here is my 2 cents on the subject to reiterate what Hal et al have previously state but also to provide some clarification on why inline code pages do not require "Handles":

Page classes
Inline code: When an ASPX page is processed by the ASP.NET runtime, the runtime dynamically generates a class named like <pagename>_aspx. This class is derived from System.Web.UI.Page. All the code in the inline code (<script runat="server">) is part of this dynamic class.

Code-behind: The @ Page directive specifies the class that the ASPX page will be derived from (Inherits="webapplication.myPage"). This class exists in the .NET project as nothing more than another class that is derived from System.Web.UI.Page. It just happens to be associated to the ASPX file within the development environment as the "code-behind" file and is compiled into the DLL.


An ASP page class has the property "AutoEventWireup". When this property is true, the ASP.NET runtime attempts to locate a predefined set of method in the class that the page is derived from. "Page_Load" is an example of this. The runtime automatically wires up the System.Web.UI.Page.Load event to this method so that it gets called when the event is fired. This is how you can have a function called Page_Load in the inline code without the "Handles" keyword (in VB.NET).

When working with the code-behind model in VB.NET, you normally has the "Handles" keyword on methods that need to be wired to page events. This performs the event wiring.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub

In C# it's done differently by means of programmatically adding EventHandler objects to the event in the InitializeComponent() method that gets called by the OnInit() method. The ASP.NET runtime calls OnInit() when it starts processing the page. Here's the C# code:

    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        //
        InitializeComponent();
        base.OnInit(e);
    }

    private void InitializeComponent()
    {
        this.Load += new System.EventHandler(this.Page_Load);
    }

The reason you need to programmatically wire the events (in either language) is because when a new web form is created the AutoEventWireup property is turned off in the @ Page directive (AutoEventWireup="False"). Without this setting being true you MUST programmatically wire the events.

I did a small test to satisfy my curiosity about these inner-workings and different coding models.
Here's the ASPX page EventWireup.aspx:

<%@ Page Language="vb" AutoEventWireup="true" src="EventWireup.aspx.vb" Inherits="CEventWireup"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <script runat="server">
            Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) ' Handles MyBase.Load
                'Put user code to initialize the page here
                Label1.Text = "This is text set in Page_Load()."
            End Sub
        </script>
        <title>Event Wireup</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <asp:label id="Label1" runat="server">This is the default Label1 text in the markup HTML.</asp:label><br>
            <asp:label id="Label2" runat="server">This is the default Label2 text in the markup HTML.</asp:label><br>
        </form>
    </body>
</html>

And here's the code-behind file EventWireup.aspx.vb:

Public Class CEventWireup
    Inherits System.Web.UI.Page

    Protected WithEvents Label2 As System.Web.UI.WebControls.Label

    Private Sub Page_Load2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Label2.Text = "This is text set in Page_Load2()."
    End Sub
End Class


Notice that I set AutoEventWireup="true". This was so I could see that both of my Page_Load methods got called. One with "Handles" commented out but with the default name of "Page_Load" so it would get picked up by the auto wiring. The other I named different, but left the "Handles" keyword on it. The resulting output is:

    This is text set in Page_Load().
    This is text set in Page_Load2().

The key is to understand when methods are getting automatically wired to the events and when they need to be programmatically wired.

Peter
-------------------------
Work smarter, not harder





Similar Threads
Thread Thread Starter Forum Replies Last Post
Create an online test using asp.net and C# Alaphat ASP.NET 2.0 Professional 3 April 14th, 2014 06:16 AM
What’s the .NET Way to test if Folder Exists? BrianWren Pro Visual Basic 2005 3 March 14th, 2007 08:35 AM
What’s the .NET Way to test if Folder Exists? BrianWren Visual Basic 2005 Basics 3 January 12th, 2007 07:32 PM
What’s the .NET Way to test if Folder Exists? BrianWren VB.NET 0 November 30th, 2006 02:46 PM
best way to test .NET source code woojtii .NET Framework 2.0 2 September 7th, 2006 07:39 PM





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