State Management
Session state has been a point of quasi-religious
debate amongst ASP programmers for years. Many methods of home-grown state
maintenance have been dreamed into existence that augment the Session objects
resident in ASP, but none have emerged as a tried and true platform for usage.
ASP.NET
provides true application state maintenance through a proprietary object model.
In essence, the burden of state maintenance has been removed from the
developer's shoulders by way of GUID-like hidden form generation in ASP.NET.
Rather than be forced to work with cookie-dependent Session objects or
spending valuable development time coming up with custom tools, developers can
rely on the powerful, built-in state maintenance resources of ASP.NET.
The
following code fragment shows a form generated in ASP.NET, with hidden controls
that hold session information that is eventually parsed and utilized by the
server once the page is sent back:
<form name="Form1" method="post"
action="CalendarPage.aspx" id="Form1">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE"
value="dDwtODk4MzA0Nzg4Ozs+WXcUq/UMVWsLI3a/7PPtuuAae2U=" />
If you
take a moment to actually step into an ASP.NET application in a
JavaScript-supporting browser, chances are you'll trace small snippets of
JavaScript code that are executed by the client and returned to the server for
use in Session maintenance.
Output Generation
From the brief look we've already taken at
server and web form controls, you've probably deduced by now that output
generation – or "working" output generation, that is – was a goal of
the ASP.NET team at Microsoft. A major hurdle in any web development strategy
and literal requirement of any web development software or framework is that
the pages generated and dumped to the client be browser-and platform-agnostic.
Microsoft has kept with its early promise
to provide this in ASP.NET. Later in this book, we'll learn more about the
power of web form controls and we'll take a deeper look at the HTML that these
controls generate. In your training with ASP.NET, take time to download various
browsers and see for yourself – you'll be hard-pressed to find a browser (with
the exception of the shell-based Lynx browser, obviously) that has a problem
loading and displaying the HTML generated by these helpful controls.
Additionally, it will become apparent that
many of the controls, when contemplated in terms of their HTML counterparts,
will generate nearly logical code fragments. If you consider just a few of the
controls in your Visual Studio.NET toolbox, and evaluate the HTML fragments
that represent these control's HTML UI, it's easy to see that ASP.NET presents
a very logical breakdown of the controls and their output.
|
Server Control
Representation
|
HTML Representation
|
|
<asp:label
id="lblArea" runat="server"/>
|
<span
id="lblArea"></span>
|
|
<asp:button
id="cmdClick" runat="server"/>
|
<input
type="submit" name="cmdClick" value=""
id="cmdClick" />
|
With so much emphasis placed on a more
control-driven paradigm than in classic ASP programming, it could conceivably
become an impossible chore to convert old ASP applications into ASP.NET
applications, with rich control adoption. Considering this, it's no mistake
that Microsoft created server controls in such a way that they reflected their
HTML counterparts' properties. To take it one step further and ease the road
for migration to a more control-driven environment, many HTML controls, when
augmented with runat="server" attribute, become server-aware objects that can be accessed through
programming logic identical to that in ASP.NET. Take a look at the following
table to see the HTML equivalents of the above examples that would yield the
same functionality and results when loaded into any web client.
|
Server-aware HTML
Representation
|
HTML Representation
|
|
<span
id="lblArea" runat="server"/>
|
<span
id="lblArea"></span>
|
|
<button
id="cmdClick" runat="server"/>
|
<input
type="submit" name="cmdClick" value=""
id="cmdClick" />
|
To show you a quick example of this
similarity in use, the following code fragment demonstrates how both server
controls and their HTML equivalents – though modified through the addition of
the runat="server" attribute –
can be embedded directly into the same ASPX page.
<%@ Page
Language="C#" %>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>SimpleControls</title>
</head>
<body>
<form id="Form1" method="post"
runat="server">
asp:label:
<asp:Label ID="lblArea" runat="server"
/><br>
asp:button:
<asp:Button ID="cmdButton"
runat="server" /><br>
</form>
HTML span: <span id="spnArea"
runat="server"/><br>
HTML button: <button id="btnCmd"
runat="server" type="button"/>
</body>
</html>
<script
runat="server">
void Page_Load()
{
lblArea.Text = "this is an asp:label";
cmdButton.Text = "this is an asp:button";
spnArea.InnerText = "this is an html span";
btnCmd.InnerText = "this is an html button";
}
</script>
The result, in any standard browser,
demonstrates how server controls and their HTML counterparts, once enabled
through exposure to server programming logic, can generate literally identical
results.
These are but a small sample of the
controls available in your web applications, but from this list you should see
that, when you get accustomed to working with the server controls, the idea of
the HTML representation of your application will be rather simple to
extrapolate.
Buy Fast Track ASP.NET here
© Copyright 2002 Wrox Press
These chapter is written by Brady Gaster, Marco Bellinaso & Kevin Hoffman
and taken from "Fast Track ASP.NET" published by Wrox Press Limited in June 2002; ISBN 1861007191; 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.
|
|