CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

Events in a datagrid, the page lifecycle and the viewstate

When it comes to building web applications ASP.NET 2.0 will make life a lot easier. The key to success in building good web apps with ASP 1.x lies in understanding the page lifecycle. Which still has a lot of secrets and surprises. In this post I will investigate it once again and cover some of my new findings.

The page life-cycle

On each roundtrip a webform is loaded in IIS and the events on the page are fired. The sequence in which the events are fired is fixed. The handling of the page request has three stages, each of the stages has its own events. You can find most of them in the property window of the forms designer.

These events are found not only on the webform itself but also on all the controls of the form.

  • In the first part the page is initialized. First the init event is fired. You can perform some initialization but its context (which contains things like the request parameters) is not avaliable yet. After this the page is loaded in IIS and the page-load event is fired. Now the context and all the controls are available. VS generates an empty event handler and advises you to write your initialization code here.

private void Page_Load(object sender, System.EventArgs e)
{
   // Put user code to initialize the page here
   // For the sake of simplicity the database code is put here :< In real life this should be somewhere else !!

   sqlConnection1.Open();
   sqlDataAdapter1.Fill(dataSet11);
   sqlConnection1.Close();
  this.DataBind();
}

  • In the second part of the cycle events like button clicks and grid-link button clicks are handled. An eventhandler, like the click of an update button, could change underlying data allready being read in in the pageload. Now you have a problem.
  • In the third part of the cycle the page will render the HTML to the result which will be returned to the client. As a last resort you can hook into the pre-render event. In case your buttons update the data the prerender event is a better place to load the data

private void WebForm1_PreRender(object sender, System.EventArgs e)
{
   // For the sake of simplicity the database code is put here. In real life this should be somewhere else.
   sqlConnection1.Open();
   sqlDataAdapter1.Fill(dataSet11);
   sqlConnection1.Close();
   this.DataBind();
}

Any updates caused by your buttonclicks are now visible on the page.

  • After the prerender event the unload and dispose event are fired. As you page no longer has a context you cannot do anything with the pages content.

A control which is not visible is not rendered. As a consequence the prerender event will not be fired on an invisible control. The page_load event is allways fired. If you put your dataloading code in the prerender event of the control consuming the data, your data will not be  read when it is not used. That’s another plus of the prerender event.

The viewstate

This scenario works as described when the viewstate is enabled, as it is by default. When you use a datagrid the viewstate can be quite huge. In case the grid’s data are read in again on every roundtrip it makes a lot of sense to disable the grid’s viewstate. This will higly reduce the amount of data travelling over the wire.
Typical events in a datagrid are things like ItemCommand which fires whenever a linkbutton (with a commandname) in the grid is clicked. Like the select or Sort button. The SelecetedIndexChanged event will fire whenever a select button was clicked. Also when the SelectedIndex has not changed in value. These events fire in the middle part of the page, together with all the other button-clicks and the like.

When you disable the viewstate the behaviour of the grid can change dramatically. In case the data is bound to the grid in the page_load, which happens before the grid’s events, the grid will behave the same. The events fire and even selectedindex has a usable value. But when you bind the data to the grid in the prerender event no events will fire at all and the SelectedIndexwill allways reads -1. Drilling down you will find that even OnBubbleEvent, which triggers the item events, does not fire. And there is nothing you can do about that.

Roundup

In an ideal situation I want the following

  • Disable the viewstate of the datagrid
  • Bind the data in the concluding part of the life cycle
  • Use the selectedindex property

The last two points are in conflict. That’s why I started working on a custom datagrid with a custom implementation of the Selectedindex. Which works for the time being. ASP.NET 2.0 will bring relief: it generates all the plumbing code needed to get my data in and out of the page and introduces the controlstate which saves the essential state of the grid without the hog.

 


Published Apr 19 2005, 03:13 AM by pvanooijen
Filed under:

Comments

John Papa said:

Peter, You hit on a topic that is near and dear to me. I find myself in discussions with deve teams often regarding hwo to load data and store it in a ASP.NET datagrid. The loading usually occurs in the Page_Load event, no problem. But then the discussion hits the point of "how do we persist the data in between post backs?" Assuming the data is specific to a particular person ...
1) Reload from the DB (difficult on paging, increased DB hits)
2) Store in Session (consumes more memory)
3) Store in Cache (not good for multiple users if data is unique)
4) Don't cache it in between. Recreate the empty DataSet and fill it from the data in the DataGrid (messy IMO, but lower resources)

This is complicated by adding in the topic of enabling/disabling of ViewState. I like to disable ViewState, but often I lose too much. I often go with option 2 or 4 (for editable, user specific rowsets). I am curious as to your thoughts on this topic.
# April 19, 2005 6:29 AM

Peter's Gekko said:

&amp;nbsp;In a recent post I wrote some things on a datagrid and what to do over roundtrips. In a comment...
# April 21, 2005 3:47 AM

Peter's Gekko said:

In my apps I organize all interaction with my databases in components. On&amp;nbsp;my component is a connection...
# May 18, 2005 7:01 AM

Peter's Gekko said:

VS 2003, VS 2005, the datagrid and controlstate
I have built a lot of web application with Visual Studio...
# August 3, 2005 3:16 PM

Peter's Gekko said:

VS 2003, VS 2005, the datagrid and controlstate
I have built a lot of web application with Visual Studio...
# August 3, 2005 3:24 PM

Peter's Gekko said:

VS 2003, VS 2005, the datagrid and controlstate
The DataGrid with Visual Studio 2003 is a very nice...
# August 15, 2005 10:43 AM

Peter's Gekko said:

Author: &lt;a href=&quot;/blogs/peter.van.ooijen&quot;&gt;Peter Van Ooijen&lt;/a&gt;&lt;br /&gt;
The DataGrid with Visual Studio 2003 is a very nice control, however, when it comes to the viewstate and &quot;pushing your app through the wire&quot; it does have some serious drawbacks. Visual Studio 2005 does go a long way towards fixing these issues, but a better general approach to optimizing DataGrid performance is fighting viewstate size.
# August 22, 2005 11:16 AM

Peter's Gekko said:

Author: &lt;a href=&quot;/blogs/peter.van.ooijen&quot;&gt;Peter Van Ooijen&lt;/a&gt;&lt;br /&gt;
The DataGrid with Visual Studio 2003 is a very nice control, however, when it comes to the viewstate and &quot;pushing your app through the wire&quot; it does have some serious drawbacks. Visual Studio 2005 does go a long way towards fixing these issues, but a better general approach to optimizing DataGrid performance is fighting viewstate size.
# August 22, 2005 11:16 AM

Agnel CJ Kurian said:

I think I will be learning PHP or Python or Ruby very very soon. I cannot spend the rest of my programming life fighting glitches in the framework. The framework is supposed to work for me. Not the other way around.
# March 20, 2006 2:19 PM

pvanooijen said:

You expect those frameworks to be glitch-less ? Nah..
And the problem I've discussed here has been solved in .NET 2.0
Which does have other glitches :)
# March 20, 2006 2:47 PM

Peter's Gekko said:

Your browser has a back, forward and refresh button. Some time ago I wrote a little post on a back button...
# May 18, 2006 4:41 AM

Bill Bomar said:

I have a question:  I am re-writing a custom control.  The original version was doing all the work via overriding the Render event.  I am re-writing it, because I needed to add some controls/events and other "intelligence" to the control.  Therefore, all child controls are now being programmatically created and added to the appropriate child collection and things are now being done in the CreateChildControls method.  This is the recommended way of doing things.  The problem I am having is this, part of the functionality of this control is to be able to download files to the client from the content management control.  Thus far, I have been unable to make the client raise the file save dialog.  The files have no physical location, rather they are being stored as objects in a database.  Any Ideas?  Thanks in advance...

# March 28, 2008 12:36 PM

aaaaaaaaaaaa said:

have a question:  I am re-writing a custom control.  The original version was doing all the work via overriding the Render event.  I am re-writing it, because I needed to add some controls/events and other "intelligence" to the control.  Therefore, all chi

# April 5, 2008 5:21 AM

pvanooijen said:

In case you really want to dive into custom controls take Nikhil Kothari and Daye's book "Developing asp.net server controls and components. Big book, by the expert.

# April 7, 2008 11:03 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

This Blog

Syndication

News