Dynamically created Datagrids data lost on Postback

Discussion in 'ASP .Net' started by ree32, Feb 18, 2005.

  1. ree32

    ree32 Guest

    I have a placeholder and depending on a user input(a drop downlist)
    when the user clicks a button I dynamically create a number of
    datagrids and fill them with data from a database. But the problem is
    that on a postback I lose all the datagrids and their data.

    I have looked at numerous pages on the net regarding this issue. Many
    say you have to rebuild build the controls on postback. How am I meant
    to do this in my situation as I don't even know the number of
    datagrids that are going to be built. I also don't want to access the
    database and retrieve the data on every single postback.

    Is there some way to load the datagrids back?
    ree32, Feb 18, 2005
    1. Advertisements

  2. ree32

    Guest Guest

    First of all you have to understand the creation of dynamic controls. From
    what you wrote, I think you are quite a bit confused about it...
    However i suggest you read this
    After that, if you still have doubts or are stuck somewhere, do post a
    message here...
    R. Thomas
    Guest, Feb 18, 2005
    1. Advertisements

  3. ree32

    ree32 Guest

    I see that the problem is that my dynamic controls are created too
    late for them to be loaded in the viewstate.

    So I changed the code to create them in Page.Load() ( I even tried it
    in page.init)

    For i As Integer = 1 To ddl.Items.Count - 1
    Dim grid As DataGrid = New DataGrid
    grid .ID = " gridRep" & i

    But now I am having problems accessing the datagrids in other parts of
    the code using.

    Sub something (n as integer).
    Dim grid As New DataGrid
    grid = CType(Me.panelReport.FindControl("gridRep" & n), DataGrid)

    But grid is coming as equal to nothing. But when I looped through all
    the controls in the Panel, the IDs were there as how I set it and as
    datagrids. But FindControl is not picking them up.

    What am I doing wrong?
    ree32, Feb 20, 2005
  4. ree32

    ree32 Guest

    Please ignore it, I put a "space" when naming the ID so it couldn't
    find it.

    Now its working I can't believe I wasted 2 hours on it, and thinking
    of the most outrageous things.
    ree32, Feb 21, 2005
  5. ree32

    ree32 Guest

    Ooops found the mistake I accidently put a space in the names of the ID.
    ree32, Feb 21, 2005
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.