Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > A viewstate of a datagrid is lost after accessing controls collection

Thread Tools

A viewstate of a datagrid is lost after accessing controls collection

Posts: n/a

The following code is an example of what not to do. It took me two
days to discover the problem. I hope it will help you to avoid the

Create a simple web form WebForm1.aspx with only two controls:
<aspataGrid id="DataGrid1" runat="server"></aspataGrid>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>

With following code-behind:

Private Function GetDataList() As ArrayList
Dim list As New ArrayList(5)

For i As Integer = 0 To list.Capacity - 1
list.Add(String.Format("Item {0}", i))

Return list

End Function

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Me.DataGrid1.DataSource = GetDataList()
End If
End Sub

Protected Overrides Function LoadPageStateFromPersistenceMedium() As
Dim viewState As Object = MyBase.LoadPageStateFromPersistenceMedium()

'Dim Count As Integer = Me.DataGrid1.Controls.Count

Return viewState
End Function

This code works as intended. Initially the datagrid is loaded from an
array. Clicking on the button causes a postback and the datagrid loads
its values from the viewstate.

Now, uncomment the middle line in the
LoadPageStateFromPersistenceMedium function. Notice that all it does
is accessing the Count property of a collection of controls in the
datagrid. By doing so, you will cause the datagrid to lose its
viewstate. Clicking on the button will yield an empty page.

Apparently, it happens because during the
LoadPageStateFromPersistenceMedium the datagrid does not have any
controls yet. Simply accessing Controls collection causes it to be
created along with few children which in its turn skews the process of
viewstate binding.

The culprit is a BaseDataList's Controls collection:
public override ControlCollection get_Controls()
return base.Controls;

So, to make this story short, if you ever need to see if a datagrid
has any controls, do not use the Controls.Count. Instead use the
HasControls() function, which is safe.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Stopping Formview insert command button - too late to trigger isvalid=false ??? viewstate lost after update Jason ASP .Net 0 11-12-2006 12:04 AM
panel's controls collection lost during postback =?Utf-8?B?RWQgQ2hpdQ==?= ASP .Net 3 11-17-2004 09:51 PM
Accessing Viewstate in dynamic user controls... =?Utf-8?B?TGFycnlC?= ASP .Net 2 08-06-2004 12:47 PM
accessing datagrid controls after databind() Bob ASP .Net Datagrid Control 0 07-15-2003 04:21 AM