Tuesday, June 30, 2009

Managing State with HTML Forms in ASP.NET - A Lightweight Alternative

Introduction

To achieve the full benefits of ASP.NET (as opposed to ASP or PHP), one generally uses the traditional Server Control - ViewState - PostBack model to speed development time by automatically handling maintaining the state of the application between form submissions and allowing the developer to focus on functionality instead of wasting his time doing things such as repopulating form fields after a postback and writing messy inline code to do it.

However, this model has disadvantages when developing interface heavy applications using complex CSS and JavaScript, and becomes especially problematic when trying to use AJAX that is not part of ASP.NET AJAX - the problem stems from the fact that when using Server Controls (or even HTML controls set to runat=server), the front-end code written by the developer often bears little resemblance to what is generated at the server. It also can be quite slow, especially when ASP.NET AJAX is introduced.

The library here presents an alternative, allowing the front end to be developed using straight HTML and / or JavaScript, using client side forms, while preserving the ability to maintain page state and to manipulate page content from server-side code without using a single inline tag. Furthermore, the problem of limited support for "Cross-Page PostBacks" has been solved - the target page that the user is sent to after form processing need not have anything to do with the submitting page.

Currently, the library, unmodified, has the following features and will work right out of the box:

  • Maintains the state of INPUT (text, password, radio, checkbox, and hidden) and SELECT tags between POSTs and / or allows their state to be set from server-side code.
  • Allows ordinary client side container elements (e.g. DIVs, SPANs) to be used as labels and/or panels, and to have their contents set on the server side.
  • Allows the developer to specify arbitrary JavaScript to run on the target page after form processing is complete.

Using the Library

The library consists of a user control, FakePostBack.ascx, to be placed in the ASPX pages for which you wish to use the library, and the FormProcessor.cs class, to be placed in your app_code folder.

I've bundled a sample "calculator" app to demonstrate usage... Let us go through the example.


See full detail: http://www.codeproject.com/KB/aspnet/viewstate-with-html-forms.aspx

2 comments:

seoinheritx said...

Thanks for your guidance, I will surely look into this solution as I am beginner to Asp.Net Development. I am slowly getting to learn the perfect solutions now.

C# Development

Naviya Nair said...

Very interesting and good Explanation on asp .net ..

ASP.NET Training
ASP NET Training
ASP.NET Online Training
Dot Net Training in Chennai
.Net Online Training
.net training online
Dot Net Online Training
C# Training