Basic stuff

S

Sparko

Hi, I am just starting to expore asp.net and I have coded a simple
HelloWorld type page using a csharp code-behind file. I have used
a <asp:Label /> server control and after wiring up my event handlers
I tried to set the Text property of the Label control in the Page_Load
method. When my page loads in the browser there is not text in the Label?
I'm sure I have declared the Label's object reference correctly. Can I
assign the label text in this way or does it have to be assigned in line?
 
S

Sparko

OK, here is the WebForm1.aspx.cs file, the aspx file is simple HTML
with the <asp:Label /> control as suggested ....

namespace dotNETDev
{
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;

/// <summary>
/// Summary description for WebForm1.
/// </summary>
///
public class WebForm1 : System.Web.UI.Page
{
protected DropDownList cmb1;
protected Label Lbl1;
protected Button Btn1;
protected Table tbl1;
protected DropDownList combo1;
protected TableRow tblrw1;

public WebForm1()
{
Page.Init += new System.EventHandler(Page_Init);

}

private void Page_Load(object sender, System.EventArgs e)
{
Lbl1.Text = "HelloWorld";

}

public void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);


}

private void Btn1_Click(object sender, EventArgs e)
{
Lbl1.Text = "Button Clicked1";



}

#region Web Form Designer generated code

/// <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);
this.Btn1.Click += new System.EventHandler(this.Btn1_Click);
}
#endregion
}
}
 
H

Hans Kesting

Sparko said:
OK, here is the WebForm1.aspx.cs file, the aspx file is simple HTML
with the <asp:Label /> control as suggested ....

namespace dotNETDev
{
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;

/// <summary>
/// Summary description for WebForm1.
/// </summary>
///
public class WebForm1 : System.Web.UI.Page
{
protected DropDownList cmb1;
protected Label Lbl1;
protected Button Btn1;
protected Table tbl1;
protected DropDownList combo1;
protected TableRow tblrw1;

public WebForm1()
{
Page.Init += new System.EventHandler(Page_Init);

}

private void Page_Load(object sender, System.EventArgs e)
{
Lbl1.Text = "HelloWorld";

}

public void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);


}

private void Btn1_Click(object sender, EventArgs e)
{
Lbl1.Text = "Button Clicked1";



}

#region Web Form Designer generated code

/// <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);
this.Btn1.Click += new System.EventHandler(this.Btn1_Click);
}
#endregion
}
}

One thing I notice is the handling of the Init event.
The code that is provided by VS.Net doesn't use a constructor
that attaches a handler to the Init event, but uses an override:

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

remove the constructor and the Page_Init method and insert the above code.
The rest of the code looks OK.

Hans Kesting
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top