Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > nested web control postback selections not updated before page_load

Reply
Thread Tools

nested web control postback selections not updated before page_load

 
 
brad
Guest
Posts: n/a
 
      02-05-2007
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

 
Reply With Quote
 
 
 
 
brad
Guest
Posts: n/a
 
      02-05-2007
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



On Feb 5, 10:41 am, "brad" <(E-Mail Removed)> wrote:
> 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



 
Reply With Quote
 
 
 
 
brad
Guest
Posts: n/a
 
      02-07-2007
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

On Feb 5, 1:35 pm, "brad" <(E-Mail Removed)> wrote:
> 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
>
> On Feb 5, 10:41 am, "brad" <(E-Mail Removed)> wrote:
>
> > 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested _Generic selections Joel C. Salomon C Programming 9 01-17-2012 03:03 AM
SqlDataSource.Selecting before Page_Load on postback? David W ASP .Net 3 02-15-2008 11:42 PM
Webpart - web user control - page_load firing as postback ryan.mclean@gmail.com ASP .Net 1 05-01-2006 09:27 PM
Why Repeater.ItemCreated is fired before Page_Load on postback Philippe Camus ASP .Net 2 03-30-2006 12:57 PM
Custom server control running code before page_load? Daves ASP .Net 10 05-11-2005 03:34 PM



Advertisments