Friday, February 10, 2012

How to create an ASP.Net web site

Step 1: Open Visual Studio
Step 2: Create a new empty web forms project, name it: HelloWorld_Web
Step 3: Add a new Web Form to the HelloWorld_Web project, named Default.aspx

At this point, you should have a file that looks like:

<form id="form1" runat="server">
<div>
</div>
</form>

Step 5: Switch over to Design mode (look at the bottom of your web page file)
Step 6: Add a Label to the page, set it's text property to "What's Your Name"
Step 7: Add a TextBox to the page, set it's Name property to txtName
Step 8: Add a LinkButton to the page, set it's Name property to lnkGetGreeting
Step 9: While on the property page for the LinkButton, go to the Events tab and double-click in the Click field. This will open up the code behind file and place an event handler in there. This is what the event handler code should look like:


void lnkGetGreeting_Click( object sender, EventArgs args )
{
  StringBuilder sb = new StringBuilder();

  sb.Append( "Hello " );

  if( txtName.Text.Length != 0 )

    sb.Append( txtName.Text )

  else

    sb.Append( "World" )


  lblGreeting.Text = sb.ToString();

}


Step 10: Go back to Default.aspx. Add a Label to the page, set it's Name proerty to lblGreeting.

Run your page, click the GetGreeting link without entering a name, you should see "Hello World" appear in the lblGreeting Label. Next, enter your name in the first text field and click on the GetGreeting link again, you should see "Hello <your name>" this time.

No comments:

Post a Comment