ASP.NET provides Web developers with the ability to efficiently
handle many tedious tasks that can eat up an application's
development time. To be able to effectively maintain state, server
controls, and debugging through the Trace object are just a few
examples of its advantages. At least 75% of my development time
involves gathering a little bit of information from the user,
querying the database, and returning some data based on that query.
The easiest way to do that usually involves splitting a page into a
few functions that return a string of HTML to the browser using
Response.Write().
With ASP.NET's new server controls, it's easier to create output to be placed into an HTML template.
This also allows for the separation of business logic and user interface. Initial
information about ASP.NET showed how easy this is to do, but it
seemed to share one downfall: the form was always redisplayed with
the output. In some cases, this is fine, or even desirable, but in
most instances, we don't want the user to see the form again. Putting
the form and the output in two different pages causes a loss of
functionality, as the controls are no longer accessible.
MSDN documentation on the server control Microsoft calls a "panel"
says the panel loosely corresponds to the HTML <DIV> tag. At first
glance it doesn't seem to offer much functionality. The key to its
worth is that it inherits the "visible" property from the "Control"
class. Setting visible equal to "False" prevents the nested contents
of the panel from being returned to the browser, but it does not
prevent ASP.NET from maintaining the state of the controls nested
within a panel. This means you can gather information from the user
in several steps and still refer to the controls,
even after multiple posts.
Sample Code
The code below demonstrates use of the panel.
<%@ Page Language="vb" %>
<script language="vb" runat=server>
Sub Page_Load(s as object, e as eventargs)
if not page.ispostback then
pnlStage1.visible = true
End if
End Sub
Sub ShowStage2(s as object, e as eventargs)
pnlStage1.visible = false
pnlStage2.visible = true
End Sub
Sub ShowStage3(s as object, e as eventargs)
pnlStage2.visible = false
pnlStage3.visible = true
lbloutput.text = "Hello, " & txtName.Text & ". How are things in " & txtCountry.text & "?"
End Sub
</script>
<html>
<head>
</head>
<body>
<form id="panel" method="post" runat="server">
<asp:panel runat=server id=pnlStage1 visible=False>
What is your name?<br>
<asp:textbox runat=server id=txtName /><br>
<asp:button runat=Server id=btnSubmit1 onclick=ShowStage2 text=submit />
</asp:panel>
<asp:panel runat=server id=pnlStage2 visible=False>
What country do you live in?<br>
<asp:textbox runat=server id=txtCountry /> <br>
<asp:button runat=server id=btnSubmit2 onclick=ShowStage3 text=submit />
</asp:panel>
<asp:panel runat=server id=pnlStage3 visible=false>
<asp:label runat=Server id=lblOutput />
</asp:panel>
</form>
</body>
</html>
Explanation
The bottom half of the page is simply the framework for the page, and
is a mix of HTML and server controls to display our form and its
output. Note that each panel's visible attribute is initially false,
and that each button's onClick event fires a corresponding Sub.
The code enclosed in the <script> tags at the top of the page is what
we're interested in. It's fairly simple and straightforward. The
Page_Load() sub checks to see if the page has been posted. If it
hasn't, the page displays the first panel. On each subsequent submit,
the sub that corresponds to the appropriate submit button is
executed. Each sub sets the visibility for the applicable panels. In
the last sub, the controls have maintained state, as evidenced by the
fact that txtName is still accessible and its text property still
reflects the changes we made two posts back.
Conclusion
Obviously, this is an overly simple example and doesn't directly
apply to a realistic application. Its purpose is to demonstrate how
the panel control can be used to gather information from users in one
or more steps and then display some sort of result based on that
information. The panel control should allow you to develop Web
applications more efficiently, and allow you to make use of
programmatic interaction with the new server controls.
About the Author
Rick Liddle is a consultant with Adjoined Technologies
(www.adjoined.com) and has been involved in ASP, Visual Basic, and
SQL Server development for about two years. Rick lives and works in
Dallas, and can be reached at rliddle@adjoined.com.
This sample chapter from Packt Publishing's "Building Websites with the ASP.NET Community Starter Kit"
illustrates how to build a new module on top of the existing code in the ASP.NET Community Starter Kit (CSK).
Using a Frequently Asked Questions (FAQ) module as an example, it shows how creating a new module allows you
to add entirely new features which integrate seamlessly with the rest of the framework. [Read This Article][Top]
Dino Esposito discusses the differences between the DataGrid control in
version 1.x and 2.0 of ASP.NET. In the process, he also builds an improved
version of the 1.x control that can get you some of the new 2.0 features
today. [Read This Article][Top]
Most default SharePoint Server Web Parts can be connected across organizations. The second article in this series shows how
to develop connectable Web Parts that provide information to other Web Parts. [Read This Article][Top]
Alex Homer continues to highlight some of the new ASP.NET 2.0 accessibility features. These features make it easier for visually impaired users to view and navigate Web sites and provide better support for alternative types of browsers and user agents. [Read This Article][Top]
Most default SharePoint Server Web Parts can be connected across organizations. The first article in this series explains how to connect existing Web Parts using the connection Interface classes in the SharePoint architecture. [Read This Article][Top]
Alex Homer highlights some of the new ASP.NET 2.0 accessibility features. These features make it easier for visually impaired users to view and navigate Web sites and provide better support for alternative types of browsers and user
agents. [Read This Article][Top]
Alex Homer discusses the simplification of, and extensions to, the ASP.NET 1.x data binding syntax, the new two-way data binding syntax for updating data sources, and the new syntax for binding to XML data in ASP.NET 2.0. [Read This Article][Top]
This article examines some of the new and exciting caching features in ASP.NET 2.0 and shows how to implement them in Web applications. [Read This Article][Top]
When implementing custom components that require access to restricted resources, implicit impersonation must be used. Jay Nathan shows how to create a class that makes using .NET Impersonation a snap. [Read This Article][Top]
Jeff Gonzalez demonstrates how to create a flexible configuration section
handler using C#. He provides a summary background of the .NET
configuration system, explains why the system is useful, and shows how it can be extended. [Read This Article][Top]
Mailing List
Want to receive email when the next article is published? Just Click Here to sign up.