Basic stuff

Discussion in 'ASP .Net' started by Sparko, Apr 19, 2005.

  1. Sparko

    Sparko Guest

    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?
     
    Sparko, Apr 19, 2005
    #1
    1. Advertising

  2. Sparko

    Karl Seguin Guest

    Sparko:
    Everything should be working, perhaps if you provided some code...

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "Sparko" <> wrote in message
    news:...
    > 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?
     
    Karl Seguin, Apr 19, 2005
    #2
    1. Advertising

  3. Sparko

    Sparko Guest

    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
    }
    }
     
    Sparko, Apr 19, 2005
    #3
  4. Sparko

    Hans Kesting Guest

    Sparko wrote:
    > 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
     
    Hans Kesting, Apr 20, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom
    Replies:
    3
    Views:
    299
    Rolf Magnus
    Dec 14, 2003
  2. wink.co.nr

    Basic Stuff

    wink.co.nr, May 16, 2007, in forum: C++
    Replies:
    4
    Views:
    305
  3. Donn Ingle

    basic if stuff- testing ranges

    Donn Ingle, Nov 25, 2007, in forum: Python
    Replies:
    12
    Views:
    425
    John Machin
    Nov 26, 2007
  4. richard

    Re: Basic array stuff

    richard, Oct 5, 2008, in forum: HTML
    Replies:
    1
    Views:
    351
    Dr J R Stockton
    Oct 5, 2008
  5. Thomas 'PointedEars' Lahn

    Re: Basic array stuff

    Thomas 'PointedEars' Lahn, Oct 6, 2008, in forum: HTML
    Replies:
    0
    Views:
    376
    Thomas 'PointedEars' Lahn
    Oct 6, 2008
Loading...

Share This Page