nested web control postback selections not updated before page_load

B

brad

Group,

I'm using Visual Studio 2003 to create an ASP.NET 1.1 project which
contains nested server user controls in order to create a tree-like
hierarchy.

The tree is a sort of question and answer dialog. The user answers a
question, and the next subquestion appears (using dynamic html
display:none|block) depending on his answer.

When all the questions are answered, the webform is posted.

Based on my reading, I understand that I have to recreate the nested
control structure in the OnInit event before the postback data is
applied to my dynamic server controls.

So, I grab my "active" question from the Session state and proceed to
rebuild my tree of controls using a method, appropriately named,
createDynamicControls.

I have verified at the end of the OnInit that the hierarchy is rebuilt
and I'm trusting the IDs to be created the same way, seeing how the
same method is used to always recreate the heirarchy.

Now, I *believe* after the init event, ASP.NET is supposed "fill-in"
the selected values in the form and then fire the page_load events for
the controls. Is this true? Because...

When I inspect my nested control tree in page_load, the state of my
radio buttons is not changed.

Architecturally, I have two custom System.Web.UI.UserControls:
QuestionManager
QuestionItem

QuestionManager has a placeholder on which QuestionItems are arranged.
QuestionItem contains two server controls:
A label for the question text
A placeholder for the answers

Answers are Radiobutton lists of possible answers. For each answer
(radio button list item), there may be a further subquestions.

If there are answer-dependent sub-questions, QuestionItems are created
for them and they are added to the parent answer placeholder
(display:none) and the display is manipulated on the client in
javascript based on the selected answer.

The tree displays nicely on the client, they can select radio buttons,
the appropriate subquestions become visible and life seems good.

--Until the postback.

A. is this a stupid architecture?
B. can it ever work?
C. why doesn't it work? i.e. why are the altered radiobuttons not
ready for me in page_load?

Thanks in advance,
Sorry if this is a newbie question on the wrong group; if it is kindly
redirect me to the appropriate group.

Brad
 
B

brad

I'll answer my own post.

The radio buttons didn't change because the IDs were not preserved. I
was depending on the generated IDs, but there are two places where the
question tree is generated:

1. In the init event on postbacks
2. In the load event on postbacks when I detect a user-initiated
change in a dropdownlist which requires a different line of
questioning.

In the inits, the assigned IDs started with ctl0_ ctl1_ etc..
However, in the loads, the assigned ids started with 1 not 0. ctl1_
ctl2_ etc even though I had done a Controls.clear to start with in
both cases.

So none of my postback data lined up with my reconstructed view
state. All I had to do was simply set the IDs in code and life was
good again. Lesson learned: never count on generated IDs when using
dynamic controls.

But it begs this question:

Can I get at postback data that doesn't re-bind to a control ID? Is
it in a collection somewhere or is it buried in the .response?



Brad
 
B

brad

And the final lesson learned: ListItems in a RadioButtonList *MUST*
contain unique values otherwise the "selected" state will not get
restored properly. It's probably in some documentation that I
skimmed, but that was the final puzzle piece.

Brad
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top