Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Page_Load(...) not executed in MainWebForm.aspx

Reply
Thread Tools

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

 
 
Richard Lionheart
Guest
Posts: n/a
 
      10-13-2005
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
}
}



 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      10-13-2005
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!



"Richard Lionheart" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> }
> }
>
>
>



 
Reply With Quote
 
 
 
 
Richard Lionheart
Guest
Posts: n/a
 
      10-15-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid : e.Item.ItemType=Pager not executed in ItemDataBound RJN ASP .Net 1 12-24-2006 10:57 PM
code on Click event for some buttons is not executed Galina Grechka ASP .Net 1 04-14-2005 04:01 AM
base.OnLoad called, OnLoad not executed Fabio R. ASP .Net 0 03-25-2005 01:21 PM
Why do LoadPostData and RaisePostDataChangedEvent not be executed? Lin ASP .Net 0 10-17-2004 11:52 AM
ASPX not executed taja ASP .Net 6 10-22-2003 08:57 PM



Advertisments