Using Dynamically Adding Controls on PostBack

B

Ben Dewey

Okay, so I have a base Page class called ArticlesPageBase. This base class
has reads in an overridable ArticleId and Loads the data into a Panel object
in the ContentPlaceHolder of the Master.

The article that it loads from the Database is a xml field that contains
html code for the article. I added some custom html to the database article
called <question id="22" />

When the article contains inline questions are parse out the start and ends
of the article to literal controls and do a
panel.Controls.Add(QuestionControl);

My problem is on PostBack my Master Article Panel container has 0 controls.

Is there a way to make these customer controls persist over postback? I
tried creating a List<QuestionControls> and adding to there in addition to
the panel, but I kept getting serialization errors.
 
C

Cowboy \(Gregory A. Beamer\)

How are you dynamically adding? If you add to a container that has ViewState
they should be there.

Question 2: What does your page load look like. Does it reset any page
elements.

Worst case is adding the info necessary to reconstitute a control properly
to viewstate and using the viewstate to rebuild the page. You then look in
your "property bag" for controls to render and render them from viewstate
values. One caveat, if you repaint and want the control to go away, you have
to clear it from ViewState as well.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside of the box!
*************************************************
 
B

Ben Dewey

Well does a panel have/persist ViewState?

My Custom Control is a using some radio button lists and text boxes that I
need to get postback info from to see whether they answered the question
right. Do I need viewstate to get PostBack data from radiobuttonlists and
textboxes?
 
B

Ben Dewey

I got it working. I had to readd the controls everytime. the issue I was
having was that I was loading them in OnLoad, i moved all the code to
OnInit and it worked.
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top