A viewstate of a datagrid is lost after accessing controls collection

Discussion in 'ASP .Net Web Controls' started by Ruslan, Nov 13, 2003.

  1. Ruslan

    Ruslan Guest

    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:
    <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
    <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.
    Ruslan, Nov 13, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William
    Replies:
    3
    Views:
    491
    [MSFT]
    Aug 30, 2004
  2. =?Utf-8?B?RWQgQ2hpdQ==?=

    panel's controls collection lost during postback

    =?Utf-8?B?RWQgQ2hpdQ==?=, Nov 17, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    610
    Kikoz
    Nov 17, 2004
  3. Jason
    Replies:
    0
    Views:
    630
    Jason
    Nov 12, 2006
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    960
    Øyvind Isaksen
    May 18, 2007
  5. Bob

    accessing datagrid controls after databind()

    Bob, Jul 15, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    114
Loading...

Share This Page