Links


www.asp.net
A brilliant collection of interactive demos and tutorials for the ASP.NET beginner

Printer friendly version of this Page

An Example ASP.NET Web Form for Data Input

Find out how ASP.NET Web Forms helps us with the tedious task of processing HTML forms

Background

We all know what HTML forms are, right? The majority of web sites contain "forms" for users to enter information... anything from simply subscribing to a newsletter, to booking a flight, to on-line share dealing.

We also know that ASP.NET is a server side technology used to create web pages and web services too. Under normal circumstances we can think of each web page as an ASP web form (or physically a '.aspx' file) on the server. This page has the extension '.aspx' because it is a web form served by ASP.NET. A web form does not have to contain a HTML form - only those pages that need user input of some kind contain HTML forms. However, if we do want to include a HTML form in our aspx web form page, then ASP.NET can help the developer immensely..

Normally, as in ASP version 3 and in other server side programming models, HTML forms are really tedious to handle. Imagine any scenario where the user must enter lots of information in a web page. What happens if the user presses 'submit', but has missed out an essential field, or has typed something invalid according to the business rules on the server (hey, we can't insure you if your hobbies include BASE jumping)? Well the whole page must be rebuilt, alerting the user to what has gone wrong, and hopefully not loosing the previously entered information that was ok. This task was up to the server side programmer to write. The latest version of ASP, ASP.NET, now automates this process, so the programmer no longer has to worry about writing code to remember all the entry fields between submits (browser to server 'round trips').


Here is an example HTML form hosted by this ASP.NET web form... enter any rubbish you like in any of the following fields and then press the 'submit' button... In this simple example ASP.NET serves the same page back, with all the fields filled in how you entered them, and in addition marking which fields have changed.

Standard Text Entry Fields
not changed not changed
not changed not changed
not changed not changed
ASP Checkbox List Control


not changed
ASP Dropdown List Control
not changed

Consider what is happening when you press Submit! The values in the form's input fields are sent (posted) to the server (somewhere in West-coast America in this case). The old page is simply thrown away by your browser whilst it awaits a brand new page. Over at the server we have programmed ASP.NET to do what ever we like with the user's 'incoming' data; the 'business logic' part if you like. Here, ASP.NET helps us because it detects exactly what fields the user has changed since last time, firing an 'event' for each change. In this example, each event handler simply relays the fact that it has fired back to the HTML page! ASP.NET then serves the page back with all the input controls automatically set to exactly the same state as the user left them. If all the input fields are deemed to be valid then we may decide to serve a completely different page back.

Here is another example; in this case one where we do not even have a 'submit' button. The calendar is an ASP.NET web form control. In the browser it is nothing more complicated than a formatted HTML table. However, when you click on it the form is submitted, events are raised at the server, and the result is a new page being served with the calendar table automatically rebuilt according to the new date selected.

ASP Calendar Control
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
No New Date Selected

The ASP text box has its 'autopostback' property on. This means that if we edit the content of the text box, then the server is alerted as soon as the user clicks outside of the box... try it!..

ASP Text box - Autopost back ON (click anywhere outside the text box to automatically submit the value)
No Key Pressed

Conclusion - A Coding Nightmare Relieved

Think how many millions of lines of code that thousands of programmers independently have had to write, and still do write, to do this same old routine task of form handling. Roll-on ASP.NET I say!.. I would though, I'm a developer! However, I do think that there are benefits to be gained all-around... i.e. Concentration of resources on the business processes that matter, quicker project development times, increased team motivation, and ultimately cost savings.

The next article presents an insight in to web forms from the developer's perspective.


 
© Copyright 2003 Mark V Williamson