How the <form> Tag Works in ASP.NET
ASP.NET has a set of form controls that can be used just like
their HTML form control equivalents, the main difference being that they are
actually constructed dynamically on the server and then sent out. There's
another difference we'll need to look at first though.
As we explained earlier when using HTML
form controls, we use a modified version of the <form> tag in ASP.NET. The ASP.NET version of the <form> tag looks like this:
<form
runat="server">
... ASP.NET form...
</form>
It takes only one attribute (runat="server"), which tells the web server that it should process
the form itself, rather than just sending it out to the browser (which won't be
able to interpret this ASP.NET-specific attribute). Even though we haven't
specified the contents of the method
and get attributes, ASP.NET is able to
handle this itself and provide its own values. In fact all ASP.NET forms are
sent by the POST method. If they are not sent by the POST method – if you try
to override it – you will not be able to use the information in the form). We
have a new version of the <form> tag, but how does that compare with the HTML version?
The <form> tag enables us to process form controls on the server. ASP.NET
introduces its own customized versions of these controls. The ASP.NET server
controls were introduced to solve many of the problems associated with the HTML
form controls. For instance, if you go forward from a form on almost any web
site, and then jump back again to make a correction, you will find that all of
the information has disappeared. This is because HTTP is stateless, as we
mentioned earlier: it has no concept of who is connecting to it, or when – it
just answers requests for a connection. However, ASP.NET now takes over some of
these responsibilities of looking after and persisting this data.
ASP.NET Server Controls
In this next section, we're going to be
demonstrating how each of the ASP.NET server controls work, and comparing the
way they are used to the way their equivalent HTML form control passed
information. We'll also demonstrate how we can start achieving our ambition of
separating the presentational code (HTML) from the code that provides the
content (ASP.NET).
The <asp:label> Control
We'll start with a small, but very useful little control, the <asp:label> control. This control provides an effective way of displaying text
on your web page in ASP.NET. It mimics the HTML <span> tag, a tag that has no effect on the text it contains, but can be
used to logically group text together into sections, paragraphs, pages, or
sentences. This control is actually vital to us if we want to separate our HTML
from our ASP.NET code.
The <asp:label> Control Attributes
The <asp:label> control is just like any normal HTML form control in that it has a collection of
attributes you can set. We won't list them all, but ones you might wish to use
are:
BackColor – sets the background color of the label
ForeColor – sets the foreground color of the label
Height – sets the
height in pixels of the label
id – sets a unique
identifier for that particular instance of the label
Text – sets the text
that
you want the label to display
Visible – determines
whether the label control is currently visible on the
page: this must be either true or false
Width – sets the
width of the label control
If you want a full detailed list of the
attributes that the <asp:label> control supports (or indeed any HTML server control), then you can
use a handy tool known as the class browser, which you can run from the
following URL:
http://www.gotdotnet.com/quickstart/aspplus/samples/classbrowser/cs/classbrowser.aspx?namespace=System.Web.UI.WebControls
On the right-hand side of the page, you can
find a list of all the controls, from label
and dropdownlist, to checkbox and the likes. Clicking on the link for a particular control will a
reveal a list of allowable attributes under the name Properties. We won't be supplying a list of attributes for the other controls,
as, generally, they each support the same attributes, and this information is
easily obtainable from the URL given above.
One other attribute not mentioned on the
lists for any of the controls, but supported by all of them is the attribute runat, which is always set to server.
This is to explicitly indicate that this particular control should be run on
the server, not the browser.
Buy Beginning ASP.NET with C# here
© Copyright 2002 Wrox Press
This chapter is written by David Sussman, et al
and taken from "Beginning ASP.NET with C#" published by Wrox Press Limited in June 2002; ISBN 1861007345; copyright © Wrox Press Limited 2002; all rights reserved.
No part of these chapters may be reproduced, stored in a retrieval system or transmitted in any form or by any means -- electronic, electrostatic, mechanical, photocopying, recording or otherwise -- without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.
|
|