ChildControls and ViewState

Discussion in 'ASP .Net' started by Graham, Sep 6, 2005.

  1. Graham

    Graham Guest

    I have created a server control that reads data from an xml file and renders either a datagrid of entries from a database or group of form controls to allow editing of an individual entry from the same database. The Xml defines how the datagrid and edit form looks and what fields each displays.
    The edit form display is perfect but I am having some problems with the datagrid list display.
    The datagrid has sorting and pagination enabled and functions correctly when used by itself.
    But when the datagrid is used with this server control it has problems with storing its data in ViewState.
    At this current time I have the server control being rendered and initialized on each page_load. This is fine the first 10 rows of data is displayed along with the 'skip to page number' pager controls of the datagrid.
    When I select another page (say page 2) of the datagrid it goes through these steps:
    1. page Posts back to the server
    2. The Page page_load function is called which calls the server controls render/init function
    3. the server controls render/init function calls the datagrids databind function, populating the datagrid with data for the first page (previous page)
    4. Any events are then processed, in this case the PageIndexChanged event (because the page number has changed from 1 to 2)
    5. the datagrids databind function is then called again to reflect the changes made by the event calls. This gets the second group of data and populates the datagrid with it.
    6. the page is then returned to the user with the second page of data showing along with the pager controls reflecting the change.

    this is all how it should be, as far as I know. I ran into trouble when trying to go back to the first page of data, and the data wouldn't change and no events fired (I would have thought the PageIndexChanged event would have considering we were attempting to change the current page).

    It turns out that on the first databind the datagrids CurrentPageIndex is not reading from ViewState (where the value is stored) properly, or at all so is always setting its initial value to 0 (Page 1). This then causes the PageIndexChanged event to fire properly on all pages that don't have an index of 0. But when we want to go back the first page the initial CurrentPageIndex = 0 then the new value also = 0 so no event is fired.

    I hope that made sense...

    Has anyone had problems similar to this that they have managed to fix?
    Any help is appreciated.
    Graham, Sep 6, 2005
    #1
    1. Advertising

  2. Hi,

    I'm not sure if this will help solve your problem but it cleared up lot of
    misunderstandings for me about the role of viewstate and exactly where it
    fits into the page lifecycle:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp

    Hope it helps.

    Julian.

    "Graham" wrote:

    > I have created a server control that reads data from an xml file and renders either a datagrid of entries from a database or group of form controls to allow editing of an individual entry from the same database. The Xml defines how the datagrid and edit form looks and what fields each displays.
    > The edit form display is perfect but I am having some problems with the datagrid list display.
    > The datagrid has sorting and pagination enabled and functions correctly when used by itself.
    > But when the datagrid is used with this server control it has problems with storing its data in ViewState.
    > At this current time I have the server control being rendered and initialized on each page_load. This is fine the first 10 rows of data is displayed along with the 'skip to page number' pager controls of the datagrid.
    > When I select another page (say page 2) of the datagrid it goes through these steps:
    > 1. page Posts back to the server
    > 2. The Page page_load function is called which calls the server controls render/init function
    > 3. the server controls render/init function calls the datagrids databind function, populating the datagrid with data for the first page (previous page)
    > 4. Any events are then processed, in this case the PageIndexChanged event (because the page number has changed from 1 to 2)
    > 5. the datagrids databind function is then called again to reflect the changes made by the event calls. This gets the second group of data and populates the datagrid with it.
    > 6. the page is then returned to the user with the second page of data showing along with the pager controls reflecting the change.
    >
    > this is all how it should be, as far as I know. I ran into trouble when trying to go back to the first page of data, and the data wouldn't change and no events fired (I would have thought the PageIndexChanged event would have considering we were attempting to change the current page).
    >
    > It turns out that on the first databind the datagrids CurrentPageIndex is not reading from ViewState (where the value is stored) properly, or at all so is always setting its initial value to 0 (Page 1). This then causes the PageIndexChanged event to fire properly on all pages that don't have an index of 0. But when we want to go back the first page the initial CurrentPageIndex = 0 then the new value also = 0 so no event is fired.
    >
    > I hope that made sense...
    >
    > Has anyone had problems similar to this that they have managed to fix?
    > Any help is appreciated
    =?Utf-8?B?SnVsaWFuIEplbGZz?=, Sep 6, 2005
    #2
    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. mauricio

    ChildControls state

    mauricio, Mar 11, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    298
    bruce barker
    Mar 11, 2008
  2. Richard Watt

    DataGrid childcontrols problem

    Richard Watt, Apr 23, 2004, in forum: ASP .Net Building Controls
    Replies:
    5
    Views:
    208
    Guest
    Apr 27, 2004
  3. Peter

    Rendered control vs childcontrols

    Peter, May 1, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    133
    Peter
    May 1, 2005
  4. Sandy

    ChildControls disappear after Clicking

    Sandy, Aug 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    119
    Sandy
    Aug 12, 2005
  5. Nico
    Replies:
    0
    Views:
    134
Loading...

Share This Page