Blog Stats
  • Posts - 15
  • Articles - 2
  • Comments - 22
  • Trackbacks - 19

 

Event problems with controls with no ViewState.

DataGrid, ViewState and SelectedIndex

The ViewState of an ASP.NET page is a hidden field that the ASP.NET framework uses to maintain the state of a web page. When a user posts a page to the server, the server will restore the contents of the controls by parsing out the state and restoring the state in the Base64 encoded field.

If you've worked with the DataGrid you know it has a tendency to increase the contents of the ViewState field by quite a bit - sometimes in excess of 100KB! To combat this, a variety of methods are employed - from overriding the Page.SavePagetoPersistenceMedium and Page.LoadPageFromPersistenceMedium and serializing it to Session state to the most obvious - setting the EnableViewState property to false. Saving the view state to Session can have scalability issues, but for web sites with minimal traffic this may be an option. The better solution IMHO is to remove disable the ViewState for the DataGrid by setting the EnableViewState property to false.

A couple of additional issues rear their heads when you do this however. For starters, you must rebind your data to the Grid on every postback instead of the traditional (!isPostBack) check. You can cache this data yourself in the Page.Cache object to mitigate the database hit issue so that is no problem. As well, you will find out that the SelectedIndex property is now one post behind. This is due to the Page Life Cycle, whereby the SelectedIndexChanged event fires after the Load event. The SelectedIndex property is set correctly but it doesn't get set until after you bind. The solution is to rebind in the OnPreRender event instead.

By setting the EnableViewState to false on my pages, I've reduced the size of the Request and Response by over 60% with no loss of funtionality.


Feedback

# re: Event problems with controls with no ViewState.

Gravatar Allen,

Thanks for the wonderful article. I was wondering does the clickevents (of hyperlinks / edit / update buttons in grid columns) gets raised, when we set viewstate to false?

Shyam 1/11/2006 7:13 AM | Shyam Arjarapu

# re: Event problems with controls with no ViewState.

Gravatar Yes, you will get a postback - but you will not receive any information on what actually occurred by default. To add some information such as a View or Edit button, you'll have to do a bit of work.

Have a button or template column
Just like the standard datagrid, you'll need to add a button column to the datagrid template - such as shown below.

<asp:ButtonColumn Text="View" ButtonType="PushButton" CommandName="VIEW">
</asp:ButtonColumn>

In order to capture the event properly on the server and identify which row they clicked on you'll have to override the data grid item created event and add the identifier to onclick reference such as show below.

private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
WebControl viewButton = (WebControl) e.Item.Cells[0].Controls[0];
viewButton.Attributes["onClick"] = Page.GetPostBackEventReference(this, String.Format("VIEW${0}", DataGrid1.DataKeys[e.Item.ItemIndex].ToString())) + ";return false;";
}
}

As well, you'll have to implement IPostBackEventHandler on your host class that has the datagrid as a child control such as below.

public class MyContainer : Control, IPostBackEventHandler
{
...
}

Then implement the IPostBackEventHandler interface by implementing the RaisePostBackEvent method and parse out the command and identifier from the eventargument.

public void RaisePostBackEvent(string eventArgument)
{
string[] keys = eventArgument.Split('$');
switch (args[0].ToUpper())
{
case "VIEW" :
// args[1] contains my identifier
// we could raise an event to the parent container or handle it directly here
break;
}
} 1/12/2006 10:49 AM | Allen Guest

# controls drag n drop problem

Gravatar sir
plz tell me the solution am suffering too much in that

asp .net controlls are not draging or droping on design mode or page even its seeing on toobox but not picking ...stting problem with IIS or any plz tell the steps to solve this problem plz email me on my id
thnks
asma
1/2/2008 3:26 PM | asma

Post a comment





 

 

 

Copyright © Allen Guest