Dynamically created Datagrids data lost on Postback

R

ree32

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?
 
R

ree32

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
panel1.Controls.Add(grid1)
grid .ID = " gridRep" & i
Loop

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?
 
R

ree32

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top