Article

ASP.NET Form Processing Basics

Page: 1 2 3 4

Our finished page in C# is as follows:

<%@ Page Language="C#" %>    
<html>    
<head>    
<title>My First ASP.NET Form</title>    
<script runat="server">    
 protected void Page_Load(Object Source, EventArgs E)    
 {    
   if (IsPostBack)    
   {    
     NameForm.Visible = false;    
     NameLabel.Text = NameBox.Text;    
     NameLabel.Style.Add( "font-weight", "bold" );    
   }    
       
   TimeLabel.Text = DateTime.Now.ToString();    
 }    
</script>    
</head>    
<body>    
   
<p>Hello <asp:label runat="server" id="NameLabel">there    
</asp:label>!</p>    
   
<form runat="server" id="NameForm">    
 <p>Do you have a name?</p>    
 <p><asp:textbox runat="server" id="NameBox" />    
    <input type="submit" value="Submit" /></p>    
</form>    
   
<p>The time is now: <asp:label runat="server" id="TimeLabel" /></p>    
   
</body>    
</html>

And here's the VB.NET version:

<%@ Page Language="VB" %>    
<html>    
<head>    
<title>My First ASP.NET Form</title>    
<script runat="server">    
 Sub Page_Load(Source As Object, E As EventArgs)    
   If IsPostBack Then    
     NameForm.Visible = false    
     NameLabel.Text = NameBox.Text    
     NameLabel.Style.Add( "font-weight", "bold" )    
   End If    
       
   TimeLabel.Text = DateTime.Now.ToString()    
 End Sub    
</script>    
</head>    
<body>    
   
<p>Hello <asp:label runat="server" id="NameLabel">there    
</asp:label>!</p>    
   
<form runat="server" id="NameForm">    
 <p>Do you have a name?</p>    
 <p><asp:textbox runat="server" id="NameBox" />    
    <input type="submit" value="Submit" /></p>    
</form>    
   
<p>The time is now: <asp:label runat="server" id="TimeLabel" /></p>    
   
</body>    
</html>

Save either of these and place it on your Web server. Load the page in your browser, and you'll see the form as expected:

Submission Form

Type in your name and submit the form, and you'll receive your personalized greeting!

Personalised Greeting

Be sure to take a minute to use the View Source feature of your browser to inspect the HTML code of each version of the page. Notice how the <asp:textbox> element in the form is actually sent to the browser as an ordinary <input type="text"> tag. Also notice that the form completely disappears once the name is submitted, and observe the style attribute that is generated to make the name bold.

The one part of the code you might not understand is the hidden __VIEWSTATE field that ASP.NET adds to the form. This is actually a pretty exciting ASP.NET feature, but it takes some explaining to understand, so you'll have to wait until a later article in this series to find out about it.

Stay Tuned...

In my next article, I'll demystify some of the object oriented programming concepts you need to understand to take full advantage of the .NET platform, including ASP.NET. I'll teach you about classes, objects, properties, and methods, and then explain the concept of inheritance. Finally, we'll use this concept to create an ASP.NET page where all the C# or VB.NET code is placed in a separate file, called a Code-Behind file.

Be sure to rate this article to let me know what you think!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: