A Server With Memory
December 4, 2000
Now, check this out! Look at the code below and now look at the
source code from the browser in the screenshot just below the code.
Is it not amazing how it automatically fills in the name and value
attributes? Okay, maybe it is not amazing, but it is still really
helpful. On a form with tons of fields you have just saved lots of
time, decreased file size and improved code readability. So how does
it work? Well, as I said before it is a server-side control, so
rather then rendering and being processed by the client, form
elements with the id's fname and lname are posted to the server and
remembered. They are sent back to the browser in an HTML 3.2
compliant form (which means it will render well on most browsers)
with all your fields filled in. Let's take a look at what is
different about the code in this page.
<html>
<head>
<title>ASP Form Sample</title>
</head>
<body>
<%
if (Len(fname.value) > 0) OR (Len(lname.value) > 0) then
Response.Write("Your name is: " & fname.value _
& " " & lname.value)
end if
%>
<form runat="server">
First Name:
<input type="text" id="fname" runat="server">
<br>
Last Name:
<input type="text" id="lname" runat="server">
<br>
<input type="submit" value="Submit Form">
</form>
</body>
</html>
First, notice how we remove the whole check at the top of the page
for whether or not the form is submitted. Instead, we have a check in
the middle of the page for the Len(fname.value) > 0 OR
Len(lname.value) > 0. In other words, the page makes sure
that fname or lname has a length of greater then nothing; however,
how is this possible without using Request.Form? To put it simply, it
is because of the manner in which .NET saves state and deals with
server controls. As you will note, each server control has an
id rather then a name. ASP.NET requires that these id's be
unique, that way it can keep track of them! As you can tell from the
resulting source code, the server uses these id's to assign the
needed attribute names and values. Furthermore, it is now possible to
reference each element by using just the ID you assigned. Wow!
Believe it or not, saving state is just the tip of the iceberg.
With some simple coding of server-side controls; form-field
validation, simple data binding and all sorts of other goodies! You
can have custom pre-coded events run with server controls, and make
even more dynamic and intelligent pages. If you have any experience
with VB Forms, then ASP.NET will be a welcome change to you, because
they introduce a similar concept coined Web Forms, which operate on
advanced techniques of the one I showed you above. What is important
to note is that none of it is very hard to do and all of it very
useful.
The Future of ASP
The .NET Revolution
There is More!
|