Introduction

When asp.net MVC came to the market, many developers simply
had switched to MVC because they have seen two major flaws in asp.net Web Form
architecture. 1. Testability and 2. Postback model. While other stuffs like
ViewState, Static Control Ids was easy to overcome but Postback model and
testability was hard to replace. I would not like to debate on separation of concerns in
this article.

This
article provides you an easy work around on how you can omit all unnecessary
life cycle events simply by using Generic Handler instead of posting data back
to the same page. Side by Side you will be able to retain the beauty and
flexibility of Lifecycle of your page for first time load or whenever needed.
In this approach, you don't need to abandon any rich Server Side Controls which
is designed to provide you a huge set of flexibility and Rapid Application
Development.

Background

This article assumes that you have a basic understanding of asp.net page life cycle and postback. You should also know a little bit about what is generic handler. You may wish to brush up your knowledge with the following articles:

PageLoad event gets triggered. You might have excluded the code execution in this by putting all your code within if(!IsPostback) condition.

Click event triggers. This is the place where you are getting values of you server controls to your entity model. Then, you save the data to the database.

Redirect to some other page.

You might be observing that your only job is to save form data to the database and then, redirect to other page. So, Instantiating controls, populating values to controls, loading view state, copying data from controls to your model are unnecessary and an overhead. If you wish to redirect to another page after saving the data, why playing with controls, ViewState and life-cycle?

Suggested Approach

The new approach suggests the idea where we don’t actually post the data back to the same page but, to a generic handler (.ashx). This would include the following steps:

To perform the first step, you can bind posted form data to the model using Bind method of ModelBinder class. In this method, I have simply looped through all the property of the model entity and fetched the corresponding form data to populate it.

The above picture is the screen shot of the demo application.

Using The Code

With this article, I have provided a sample which performs the basic CRUD operations on Employee using Fast Postback approach. The sample also provides you an easy way to bind form data with entity and entity data with controls.

Here I assume that all the required authentication and authorizations have been processed at HttpModule level. Else, you may wish to consider manual authentication/authorization in your Handler.

The above picture is the screen shot of the demo application.

Model Binding

The sample code provided also provides a basic approach to bind models with Controls and form data. You may extend this code to implement things based on your need. If you wish to implement the model binding at a detail level you may wish to consider this link.

Points of Interest

The provided sample has also used FriendlyUrls for asp.net. So, for beginners, it might act as a sample on how you can easily generate Restful URL in Asp.Net web forms.

The provided sample also uses entity framework. So, for beginners, it might act as a very light-weight sample on using entity framework.

The attached code can also be used as a sample for beginners on how a generic handler can be used for such kind of operation in a situation where only a set of code processing is needed at server side.

Future Consideration

Well, this article has just provided you the idea and sample implementation, I am planning to create a small library to handle more complex situations also with this approach. Some of them include:

Binding more complex controls with model.

Binding form data to IEnumerables.

Transferring the control back to the page when exception/error occurred.

Conclusion

Wherever you want to improve performance of your web form application, you can easily switch to this approach for simple web forms along with taking advantage of page LifeCycle and server controls during first time load. Moreover, this approach may exists side by side with the existing post back approach for some complex pages where you may not wish to implement this approach.

I always welcome your feedback/comment/suggestions to improve it further.

Share

About the Author

Anurag Gandhi currently works in web application design/development and has done so for many years now as he is passionate about programming.
He is extensively involved in Asp.Net and Asp.Net MVC web application architecture, AngularJs, design and development. His languages of choice are C#, Asp.Net, MVC, Asp, C, C++. But he is familiar with many other coding languages. He works with MS Sql Server as the database architecture of choice.
He is active in programming communities and loves to share his knowledge with other developers whenever he gets the opportunity.
He is also a passionate chess player.

A good example for beginners to learn reflect API , and also can learn a good handy way for binding between entity and controls .
I have wrote similar codes in winform before , after reading yours , allow me to provide a little advices :
1. if Control's id was not named by property name ?
2. bind more than one model ?
3. when using Conveter.ChangeType , some necessary exception handing？ take converting a specicallu formatted datetime string to datetime , or a integer bigger than int.Max to int as example .
4. display formatting control when binding entity to controls , such as "mm-dd-yyyy" , "#000,00.00" .
I haven't solved above yet , maybe you will , or others have seen it may have interesting .