Pages

Thursday, May 20, 2010

Displaying ‘Hello world’ message using C#.

This tutorial demonstrates how to print/display message on screen. This is meant for novice programmers.

Open aspx page of the new website. Drag 1 label & 1 button from the toolbox at the left.
Have a look at images below. When you click on the button, the message is displayed.




On the button click we will get result like this





C# Code-

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class HelloWorld : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Event fires when page loads on each control event
}
protected void btn_Click(object sender, EventArgs e)
{
lblMessage.Text = "Hello World. Welcome to ASP.NET";
}
}

'lblMessage' is the name of label control
Here a simple label is shown on the button click event.
Page_load is a function which is triggered on the loading of page. So if you want to print the message right after the loading of page, then instead of using button click event, write that code in page_load section!

Click here to download complete code. Enjoy.

No comments:

Post a Comment