R
Ruslan
Hello,
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
same.
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))
Next
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()
Me.DataGrid1.DataBind()
End If
End Sub
Protected Overrides Function LoadPageStateFromPersistenceMedium() As
Object
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()
{
base.EnsureChildControls();
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.
Regards,
Ruslan.
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
same.
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))
Next
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()
Me.DataGrid1.DataBind()
End If
End Sub
Protected Overrides Function LoadPageStateFromPersistenceMedium() As
Object
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()
{
base.EnsureChildControls();
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.
Regards,
Ruslan.