Page_Load(...) not executed in MainWebForm.aspx

R

Richard Lionheart

Hi All,

I adapted some sample code from w3schools.com and tried to adapt it to .NET
to test dynamic processing in a WebForm. The client page that got generated
produced everything that was coded except for displaying the time at which
it was generated. I tried everything I could think of to rewrite this and
to debug this.

Anyone out there in Cyberspace got any idea why the time doesn't get
displayed. The .aspx code follows below

Thanks in Advance,
Richard

======== MainWebForm.aspx ===============
<%@ Page language="c#" Codebehind="MainWebForm.aspx.cs"
AutoEventWireup="false" Inherits="TestDynamicTimeReporting2.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Dynamic-Time Form</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">

<script language="C#" runat="server">
void Page_Load(Object Src, EventArgs E) {
MyTimeLabel.Text = "You last accessed this page at: " +
DateTime.Now;
}
</script>

</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="MyForm" method="post" runat="server">
<asp:Label id="MyTopLabel" style="Z-INDEX: 101; LEFT: 160px;
POSITION: absolute; TOP: 19px" runat="server" Width="429px" Height="25px">
<h1>Test Dynamic Time Reporting</h1>
</asp:Label>
<asp:Label id="MyTimeLabel" style="Z-INDEX: 102; LEFT: 291px;
POSITION: absolute; TOP: 102px" runat="server" Width="190px" Height="22px"
ForeColor="Red">The time is ...........................</asp:Label>
<asp:Button id="MyButton" style="Z-INDEX: 103; LEFT: 326px;
POSITION: absolute; TOP: 198px" runat="server" Width="106px" Height="33px"
Text="Update the time"></asp:Button>
</form>
</body>
</HTML>


=================== MainWebForm.aspx =========================

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TestDynamicTimeReporting2
{
/// <summary>
/// Summary description for MainWebForm.
/// </summary>
public class MainWebForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label MyTopLabel;
protected System.Web.UI.WebControls.Label MyTimeLabel;
protected System.Web.UI.WebControls.Button MyButton;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
 
K

Karl Seguin

If you want ur <Script runat="Server"...> to work, you need to set
AutoEventWireUp to true in the @Page directive.

I don't think there's any good reason to use both inline code (<script
runat="server">...</script>) and codebehind (the MainWebForm.aspx.cs)
Decide which methodology you want to use and stick with it (I prefer the
codebehind, and so does Visual Studio .Net). Therefore, if you simply
removed ur <script runat="Server"... and put the MyTimeLabel.Text = "You
last accessed this page at: " + DateTime.Now in the Page_Load of the .cs
file and recompiled everything should work.

Karl
 
R

Richard Lionheart

Hi Karl,

Thank you for the perfect answer to my quandary. I will clean up this
example I got from W3Schools.com, both ways -- one using script exclusively
and the other only code-behind -- just so I'm up-to-speed on this aspect.

Thank you also for you references to openmind.net. I am interested in XML
under .NET, so AJAX is right up my alley.

Instead of just copying your corrections, I made modifications myself,
which led to one more question about the scripting version. What I did was:

1. I modified the AutoEventWireup as you suggested and that of course broke
the log jam.

2. The original example presented a refreshed server-built client page every
time the button was clicked, which led the then-current time at the server
to be presented on the client page.

3. I updated the example to have original-server-time for the first client
page presented with every updated client page. If I had known how, I would
have created a static variable initialized to upon creation of the first
page. The not-really-so-kludgey way I did was to modify OnPageLoad as
follows:
void Page_Load(Object Src, EventArgs E) {
MyTimeLabel.Text = "You updated this page at: " + DateTime.Now;
if (MyTextLabel.Text == "")
{ MyTextLabel.Text = "The orignal page was cre-ated" +
DateTime.Now; }
}

Was there a simple way do define and initialize a static variable with the
time of the first page's creation. In scripting? While we're at it, how
about in code-behind?

Thanks again,
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top