Build Your Own ASP.NET Website Using C# and VB.NET. Pt. 4. | 3
Build Your Own ASP.NET Website Using C# and VB.NET. Pt. 4.
Processing a Simple Form
Now that you have a basic understanding of ASP.NET page structure, the languages VB.NET and C#, and HTML controls, let’s put everything together and create a simple ASP.NET application. The application that we will create, in VB.NET and C#, will be a simple survey form that uses the following HTML controls:
HtmlForm
HtmlButton
HtmlInputText
HtmlSelect
Let’s begin by creating a new file within your favorite code editor. The following code creates the visual interface for the survey:
Example 4.1. SimpleForm.aspx (excerpt)<html>
<head>
…
</head>
<body>
<form runat="server">
<h2>Take the Survey!</h2>
<p>Name:<br />
<input type="text" id="txtName" runat="server" /></p>
<p>Email:<br />
<input type="text" id="txtEmail" runat="server" /></p>
<p>Which server technologies do you use?<br />
<select id="servermodel" runat="server" multiple="true">
<option>ASP.NET</option>
<option>PHP</option>
<option>JSP</option>
<option>CGI</option>
<option>Coldfusion</option>
</select></p>
<p>Do you like .NET so far?<br />
<select id="likedotnet" runat="server">
<option selected>Yes</option>
<option>No</option>
</select></p>
<p><button id="myButton" OnServerClick="Click" runat="server">
Confirm</button></p>
</form>
</body>
</html>
From what we’ve already covered on HTML controls, you should have a good idea of what this page will look like. All we’ve done is place some HtmlInputText controls, an HtmlButton control, and an HtmlSelect control inside the obligatory HtmlForm control. Remember, HTML controls are essentially just HTML tags with the runat="server" attribute. When it’s complete, the interface will resemble Figure 4.1.
When users click the button, we’ll simply display their responses in their browsers. In a real application, we’d probably be more likely to save this to a database and perhaps show the results as a chart. Whatever the case, we’d access the properties of the HTML controls as shown in the following code:
Example 4.2. SimpleForm.aspx (excerpt)
<script runat="server" language="VB">
Sub Click(s As Object, e As EventArgs)
Response.Write("Your name is: " & txtName.value & "<br />")
Response.Write("Your email is: " & txtEmail.value & "<br />")
Response.Write("You like to work with: " & servermodel.value & _
"<br />")
Response.Write("You like .NET: " & likedotnet.value)
End Sub
</script>Example 4.3. SimpleForm.aspx (excerpt)
<script runat="server" language="C#">
void Click(Object s, EventArgs e) {
Response.Write("Your name is: " + txtName.Value + "<br />");
Response.Write("Your email is: " + txtEmail.Value + "<br />");
Response.Write("You like to work with: " + servermodel.Value +
"<br />");
Response.Write("You like .NET: " + likedotnet.Value);
}
</script>Just as you’ve seen with examples from previous chapters, we place our VB.NET and C# code inside a server-side script block within the <head> part of the page. Next, we create a new Click event handler which takes the two usual parameters. Finally, we use the Response object’s Write() method to print out the user’s responses within the page.
Once you’ve written the code, you can save your work and test the results from your browser. Enter some information and click the button. What you type in should appear at the top of the page when the button is clicked.
With the inception of new technologies, there’s always new terminology to master. ASP.NET is no different. With ASP.NET, even the simplest terms that were previously used to describe a Web page have changed to reflect the processes that occur within them. Before we begin to describe the process followed by Web Forms, let’s discuss the foundation concept of Web pages.
On the most basic level, a Web page is a text file that contains markup. Web pages are meant to be viewed from a browser window, which parses the file containing markup to present the information to the user in the layout envisaged by the developer. Web pages can include text, video, sound, animations, graphics, and even chunks of "code" from a variety of technologies.
An HTML form, as you learned in the previous sections, is a page that contains one or more form elements grouped together within an HTML <form> tag. Users interact with the various form elements to make certain choices, or provide certain information; this information is then sent to the server for processing upon the click of a submit button. This is useful to us as ASP.NET developers because regular HTML forms have a built-in mechanism that allows forms to be submitted to the server. Once the form has been submitted, some kind of extra technology—in this case, ASP.NET—needs to be present on the server to perform the actual form processing.
In ASP.NET, we call Web pages Web Forms; they contain presentational elements (ASP.NET Web controls) in an HTML form, as well as any code (the processing logic) we’ve added for the page’s dynamic features.
A typical Web Form is shown in Figure 4.2:
Figure 4.2. A Web Form contains code for processing logic and Web controls for presentational purposes.

The next section looks at the various Web controls and how they may be used within your Web Forms. They’re very similar in appearance to HTML, so you shouldn’t have any trouble coming to grips with them.
Created: March 27, 2003
Revised: July 12, 2004
URL: http://webreference.com/programming/asp_net4/1


Find a programming school near you