nested web control postback selections not updated before page_load

Discussion in 'ASP .Net' started by brad, Feb 5, 2007.

  1. brad

    brad Guest

    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
    brad, Feb 5, 2007
    #1
    1. Advertising

  2. brad

    brad Guest

    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" <> 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
    brad, Feb 5, 2007
    #2
    1. Advertising

  3. brad

    brad Guest

    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" <> 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" <> 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
    brad, Feb 7, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philippe Camus
    Replies:
    2
    Views:
    2,504
    Philippe Camus
    Mar 30, 2006
  2. Replies:
    1
    Views:
    2,604
  3. David W
    Replies:
    3
    Views:
    839
    David W
    Feb 15, 2008
  4. Dmitry Korolyov

    Datagrid not updated during delete, but updated during insert and update

    Dmitry Korolyov, Sep 22, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    407
    Dmitry Korolyov
    Sep 22, 2003
  5. Joel C. Salomon

    Nested _Generic selections

    Joel C. Salomon, Jan 13, 2012, in forum: C Programming
    Replies:
    9
    Views:
    453
    Shao Miller
    Jan 17, 2012
Loading...

Share This Page