dynamic controls - disapear after postback

C

Chris Thunell

I have created several grids dynamically and have added them to different
HTML placeholders on a vb.net web form. The grids and controls within them
come up and view beautifully when the web page first starts, but when I
click on a button, and the page comes back, my dynamically created grid and
controls are not visible. Any thoughts? Please view my sample code below.

If Not ispostback then
creategrids
end if

Sub CreateGrids
'lots of code above
phInsulation.Controls.Add(MatrixInsul) 'phinsulation is a placeholder
and MatrixInsul is my grid
phGC.Controls.Add(MatrixGC)
phSM.Controls.Add(MatrixSM)
phControls.Controls.Add(MatrixControls)
phInsulation.EnableViewState = True
phGC.EnableViewState = True
phSM.EnableViewState = True
phControls.EnableViewState = True

End Sub

Thanks in advance for your help!
Chris Thunell
(e-mail address removed)
 
S

Steve Flitcroft

Because the controls are not generated when you postback the form (e.g when
you click the button the CreateGrid function will not fire thus the form
will render without the controls)
 
M

Marina

Yes - you need to call creategrids every time the page loads - not just the
first time. Dynamically created controls are not going to automatically be
recreated for you.
 
M

Mark Fitzpatrick

It's pretty simple. They're generated the first time, but during postback
you are not calling the CreateGrids function as you've told it not to with
the if not ispostback statement, thus they aren't getting created on
subsequent calls. You need to toy with the determination of what isn't
required during a postback. It could be that in certain button click events
you'll want to add a call to the CreateGrids sub while in others you won't.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
C

Chris Thunell

How do i get the information from the controls before RECREATING the
controls? When the form first loads with all the controls, i enter some
information in these dynamic controls. So i need to get that information
out of the dynamic control, before i recreate teh control with blank
information again. When i push my button to "save" the information... the
controls are already gone and i can't access their information
Help help! :)
Chris Thunell
(e-mail address removed)
 
M

Marina

I don't know the details of your conrols, but recreating them should be
enough for viewstate to do its thing and populate the controls with data.
 
C

Chris Thunell

Please read what i sent to Marina in the group.
Thanks!
Chris Thunell
(e-mail address removed)
 
C

Chris Thunell

If i recreate the controls.. i get an error:
Multiple controls with the same ID 'cbMechSubAwarded_5_736' were found.
 
B

Bryant Hankins

Chris,

In order to retrieve the data, you need to ensure you add the user control
dynamically each time (ie - don't only add it the first time) to the same
place on the page. Also, you should create and add the dynamic control in
the Page_Init rather than in the Page_Load. Once you do that the control
will always be there and you'll always be able to get the values.

--
Hope this helps,
Bryant Hankins
Numinet Systems Inc.
http://www.numinet.com
 
J

Jim Corey

You'll want to add a distinct id to each
control each time you create it,
for example,
MatrixGC.ID = "MatrixGC"

Jim
 
C

Chris Thunell

If I apply a distinct id to each control... how will i find that specific
control again in the future (to get data FROM the control)... I'm thinking
of creating an arraylist with the control id and other pertinent data)

Any other suggestions?
 
A

Andrea Williams

THX! I don't know if it helped Chris's problem, but it helped mine. I was
creating my grid and rebinding my data to the grid in the Page_Load on every
pass or I'd see nothing in the grid and the events wouldn't fire. But of
course rebinding the data made me loose the changes. I moved my
CreateGrid() method to the OnInit() method and now I don't have to rebind
the grid and I don't loose my events.

I have no idea it was going to be such a problem to create the grid
dynamically. I think this will solve a random error that my tester is
seeing.

Thanks, again!
Andrea
 
C

Chris Thunell

Thank you all for your help! I have moved my CreateGrid function to OnInit,
and i also created a new class of web controls through inheritance to handle
additional information for each item. So on the round trip back, i can go
through each of the controls and find out what the control data was for.
aka textbox1.BidNo = xxx, textbox1.JobNo= xxx...

Thank you everyone for your patience in helping this newbie programmer out!
Chris Thunell
(e-mail address removed)
 
A

Andrea Williams

Another note. If you are creating a dynamic form in a User control, the
CreateGrid() should be put in the Page_Load method, not the OnInit(). If
added to OnInit(), the events fail to fire. At least that 's the behavior
I get....

Andrea
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top