Programmatically added controls losing state

Discussion in 'ASP .Net' started by mark.norgate@gmail.com, Aug 15, 2006.

  1. Guest

    I've asked this question before, but still haven't solved it, so am
    asking again.

    I am programmatically adding a user control to the page in response to
    a button click. The user control consists of three dropdowns and seven
    text boxes.

    When the button is clicked, I add another control to the page in Click
    event of the button and populate the three dropdowns. The text boxes
    are to be populated by the user.

    All ok so far, but when I click the button again, since it is a
    postback, I do not rebind the data to the dropdowns and their options
    disappear, although the seven text boxes maintain their state.

    Can anyone explain why this might be?

    Ta, Mark
     
    , Aug 15, 2006
    #1
    1. Advertising

  2. Since the Control is dynamically added, it is not referenced in the class
    definition. Since the Page class must be rebuilt from scratch with each
    PostBack, the dynamically-created Control must be manually restored with
    each PostBack.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    Chicken Salad Surgery

    Orange you bland I stopped splaying bananas?


    <> wrote in message
    news:...
    > I've asked this question before, but still haven't solved it, so am
    > asking again.
    >
    > I am programmatically adding a user control to the page in response to
    > a button click. The user control consists of three dropdowns and seven
    > text boxes.
    >
    > When the button is clicked, I add another control to the page in Click
    > event of the button and populate the three dropdowns. The text boxes
    > are to be populated by the user.
    >
    > All ok so far, but when I click the button again, since it is a
    > postback, I do not rebind the data to the dropdowns and their options
    > disappear, although the seven text boxes maintain their state.
    >
    > Can anyone explain why this might be?
    >
    > Ta, Mark
    >
     
    Kevin Spencer, Aug 15, 2006
    #2
    1. Advertising

  3. Just to add to that, you can use Denis Bauer's DynamicControlsPlaceholder to
    hepl you do this (free):
    http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

    or you can roll the logic urself, which isn't too hard. Normally people
    store the this value in the ViewState:

    LoadControl("asdsa.ascx");
    ViewState.Add("ControlLoaded", true);

    and then check on postback:

    if (Page.IsPostBack AND (bool)ViewState["ControlLoaded"])
    {
    ReloadTheControl();
    }

    if you reloadthecontrol during or before onLoad, state should be maintained.

    Karl

    --
    http://www.openmymind.net/
    http://www.codebetter.com/


    "Kevin Spencer" <> wrote in message
    news:...
    > Since the Control is dynamically added, it is not referenced in the class
    > definition. Since the Page class must be rebuilt from scratch with each
    > PostBack, the dynamically-created Control must be manually restored with
    > each PostBack.
    >
    > --
    > HTH,
    >
    > Kevin Spencer
    > Microsoft MVP
    > Chicken Salad Surgery
    >
    > Orange you bland I stopped splaying bananas?
    >
    >
    > <> wrote in message
    > news:...
    >> I've asked this question before, but still haven't solved it, so am
    >> asking again.
    >>
    >> I am programmatically adding a user control to the page in response to
    >> a button click. The user control consists of three dropdowns and seven
    >> text boxes.
    >>
    >> When the button is clicked, I add another control to the page in Click
    >> event of the button and populate the three dropdowns. The text boxes
    >> are to be populated by the user.
    >>
    >> All ok so far, but when I click the button again, since it is a
    >> postback, I do not rebind the data to the dropdowns and their options
    >> disappear, although the seven text boxes maintain their state.
    >>
    >> Can anyone explain why this might be?
    >>
    >> Ta, Mark
    >>

    >
    >
     
    Karl Seguin [MVP], Aug 15, 2006
    #3
  4. Guest

    I am restoring my user control...but (some of) its child controls are
    not maintaining their state. Why should the textboxes do so, but not
    the dropdowns?

    Mark

    Kevin Spencer wrote:
    > Since the Control is dynamically added, it is not referenced in the class
    > definition. Since the Page class must be rebuilt from scratch with each
    > PostBack, the dynamically-created Control must be manually restored with
    > each PostBack.
    >
    > --
    > HTH,
    >
    > Kevin Spencer
    > Microsoft MVP
    > Chicken Salad Surgery
    >
    > Orange you bland I stopped splaying bananas?
    >
    >
    > <> wrote in message
    > news:...
    > > I've asked this question before, but still haven't solved it, so am
    > > asking again.
    > >
    > > I am programmatically adding a user control to the page in response to
    > > a button click. The user control consists of three dropdowns and seven
    > > text boxes.
    > >
    > > When the button is clicked, I add another control to the page in Click
    > > event of the button and populate the three dropdowns. The text boxes
    > > are to be populated by the user.
    > >
    > > All ok so far, but when I click the button again, since it is a
    > > postback, I do not rebind the data to the dropdowns and their options
    > > disappear, although the seven text boxes maintain their state.
    > >
    > > Can anyone explain why this might be?
    > >
    > > Ta, Mark
    > >
     
    , Aug 15, 2006
    #4
  5. Guest

    Karl

    Would you mind explaining this in a little more detail? I'm reluctant
    to use a third-party component because I want to know what the problem
    is for myself so I can increase my understanding.

    Why is it that some of the controls in my user control (text boxes)
    maintain their state, yet others (drop downs) do not?

    And I'm not entirely convinced I understand the function of the
    ReloadTheControl() method in your example, or how to distinguish one
    control from another using ViewState.Add("ControlLoaded", true), since
    there many be many of these user controls on the page.

    Thanks, Mark

    Karl Seguin [MVP] wrote:
    > Just to add to that, you can use Denis Bauer's DynamicControlsPlaceholder to
    > hepl you do this (free):
    > http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
    >
    > or you can roll the logic urself, which isn't too hard. Normally people
    > store the this value in the ViewState:
    >
    > LoadControl("asdsa.ascx");
    > ViewState.Add("ControlLoaded", true);
    >
    > and then check on postback:
    >
    > if (Page.IsPostBack AND (bool)ViewState["ControlLoaded"])
    > {
    > ReloadTheControl();
    > }
    >
    > if you reloadthecontrol during or before onLoad, state should be maintained.
    >
    > Karl
    >
    > --
    > http://www.openmymind.net/
    > http://www.codebetter.com/
    >
    >
    > "Kevin Spencer" <> wrote in message
    > news:...
    > > Since the Control is dynamically added, it is not referenced in the class
    > > definition. Since the Page class must be rebuilt from scratch with each
    > > PostBack, the dynamically-created Control must be manually restored with
    > > each PostBack.
    > >
    > > --
    > > HTH,
    > >
    > > Kevin Spencer
    > > Microsoft MVP
    > > Chicken Salad Surgery
    > >
    > > Orange you bland I stopped splaying bananas?
    > >
    > >
    > > <> wrote in message
    > > news:...
    > >> I've asked this question before, but still haven't solved it, so am
    > >> asking again.
    > >>
    > >> I am programmatically adding a user control to the page in response to
    > >> a button click. The user control consists of three dropdowns and seven
    > >> text boxes.
    > >>
    > >> When the button is clicked, I add another control to the page in Click
    > >> event of the button and populate the three dropdowns. The text boxes
    > >> are to be populated by the user.
    > >>
    > >> All ok so far, but when I click the button again, since it is a
    > >> postback, I do not rebind the data to the dropdowns and their options
    > >> disappear, although the seven text boxes maintain their state.
    > >>
    > >> Can anyone explain why this might be?
    > >>
    > >> Ta, Mark
    > >>

    > >
    > >
     
    , Aug 15, 2006
    #5
  6. Mark:
    Textboxes are very unique in their ability to retain their state. They'll
    retain their state via Request.Form, rather than the ViewState like most
    (all?) other controls - that's because they are single valued. Don't be
    thrown off by this unique behaviour :)



    ReloadTheControl() was just a stub function...all it would do is something
    like:

    Blah.Controls.Add(Page.LoadControl("adsadsa.ascx"))..


    if you have multiple controls, you'll need to store a more complicated
    object in the viewstate...such as an arraylist of strings...


    public void AddDynamicControl(string path)
    {
    ArrayList controlsToReload = (ArrayList)ViewState["dynamicControls"];
    if (controlsToReload == null)
    {
    controlsToReload = new ArrayList();
    ViewState.Add("dynamicControls", controlsToReload);
    }
    controlsToReload .Add(Path);
    }

    so whever you dynamically add a new control, you call AddDynamicControl

    LoadControl("whatever.ascx");
    AddDynamicControl("whatever.ascx");


    Then on PostBack, you can reload all the controls:

    public vod ReloadDynamicControls()
    {
    ArrayList controslToReload = (ArrayList)ViewState["dynamicControls"];
    if (controslToReload ! = null)
    {
    foreach (string path in controslToReload)
    {
    Page.LoadControl(path);
    }
    }
    }

    Anyways, all of this is just some code off hte top of my head, you'll likely
    need to fx it up a bit/a lot :)

    Karl

    --
    http://www.openmymind.net/
    http://www.fuelindustries.com/


    <> wrote in message
    news:...
    > Karl
    >
    > Would you mind explaining this in a little more detail? I'm reluctant
    > to use a third-party component because I want to know what the problem
    > is for myself so I can increase my understanding.
    >
    > Why is it that some of the controls in my user control (text boxes)
    > maintain their state, yet others (drop downs) do not?
    >
    > And I'm not entirely convinced I understand the function of the
    > ReloadTheControl() method in your example, or how to distinguish one
    > control from another using ViewState.Add("ControlLoaded", true), since
    > there many be many of these user controls on the page.
    >
    > Thanks, Mark
    >
    > Karl Seguin [MVP] wrote:
    >> Just to add to that, you can use Denis Bauer's DynamicControlsPlaceholder
    >> to
    >> hepl you do this (free):
    >> http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
    >>
    >> or you can roll the logic urself, which isn't too hard. Normally people
    >> store the this value in the ViewState:
    >>
    >> LoadControl("asdsa.ascx");
    >> ViewState.Add("ControlLoaded", true);
    >>
    >> and then check on postback:
    >>
    >> if (Page.IsPostBack AND (bool)ViewState["ControlLoaded"])
    >> {
    >> ReloadTheControl();
    >> }
    >>
    >> if you reloadthecontrol during or before onLoad, state should be
    >> maintained.
    >>
    >> Karl
    >>
    >> --
    >> http://www.openmymind.net/
    >> http://www.codebetter.com/
    >>
    >>
    >> "Kevin Spencer" <> wrote in message
    >> news:...
    >> > Since the Control is dynamically added, it is not referenced in the
    >> > class
    >> > definition. Since the Page class must be rebuilt from scratch with each
    >> > PostBack, the dynamically-created Control must be manually restored
    >> > with
    >> > each PostBack.
    >> >
    >> > --
    >> > HTH,
    >> >
    >> > Kevin Spencer
    >> > Microsoft MVP
    >> > Chicken Salad Surgery
    >> >
    >> > Orange you bland I stopped splaying bananas?
    >> >
    >> >
    >> > <> wrote in message
    >> > news:...
    >> >> I've asked this question before, but still haven't solved it, so am
    >> >> asking again.
    >> >>
    >> >> I am programmatically adding a user control to the page in response to
    >> >> a button click. The user control consists of three dropdowns and seven
    >> >> text boxes.
    >> >>
    >> >> When the button is clicked, I add another control to the page in Click
    >> >> event of the button and populate the three dropdowns. The text boxes
    >> >> are to be populated by the user.
    >> >>
    >> >> All ok so far, but when I click the button again, since it is a
    >> >> postback, I do not rebind the data to the dropdowns and their options
    >> >> disappear, although the seven text boxes maintain their state.
    >> >>
    >> >> Can anyone explain why this might be?
    >> >>
    >> >> Ta, Mark
    >> >>
    >> >
    >> >

    >
     
    Karl Seguin [MVP], Aug 15, 2006
    #6
  7. Guest

    Ok, thanks a lot for taking the time Karl.

    I have found that by binding the data to the drop downs, even when
    posting back, the selected values of the drop downs is maintained,
    which is exactly the behaviour I want.

    Thanks for your help!

    Mark

    Karl Seguin [MVP] wrote:
    > Mark:
    > Textboxes are very unique in their ability to retain their state. They'll
    > retain their state via Request.Form, rather than the ViewState like most
    > (all?) other controls - that's because they are single valued. Don't be
    > thrown off by this unique behaviour :)
    >
    >
    >
    > ReloadTheControl() was just a stub function...all it would do is something
    > like:
    >
    > Blah.Controls.Add(Page.LoadControl("adsadsa.ascx"))..
    >
    >
    > if you have multiple controls, you'll need to store a more complicated
    > object in the viewstate...such as an arraylist of strings...
    >
    >
    > public void AddDynamicControl(string path)
    > {
    > ArrayList controlsToReload = (ArrayList)ViewState["dynamicControls"];
    > if (controlsToReload == null)
    > {
    > controlsToReload = new ArrayList();
    > ViewState.Add("dynamicControls", controlsToReload);
    > }
    > controlsToReload .Add(Path);
    > }
    >
    > so whever you dynamically add a new control, you call AddDynamicControl
    >
    > LoadControl("whatever.ascx");
    > AddDynamicControl("whatever.ascx");
    >
    >
    > Then on PostBack, you can reload all the controls:
    >
    > public vod ReloadDynamicControls()
    > {
    > ArrayList controslToReload = (ArrayList)ViewState["dynamicControls"];
    > if (controslToReload ! = null)
    > {
    > foreach (string path in controslToReload)
    > {
    > Page.LoadControl(path);
    > }
    > }
    > }
    >
    > Anyways, all of this is just some code off hte top of my head, you'll likely
    > need to fx it up a bit/a lot :)
    >
    > Karl
    >
    > --
    > http://www.openmymind.net/
    > http://www.fuelindustries.com/
    >
    >
    > <> wrote in message
    > news:...
    > > Karl
    > >
    > > Would you mind explaining this in a little more detail? I'm reluctant
    > > to use a third-party component because I want to know what the problem
    > > is for myself so I can increase my understanding.
    > >
    > > Why is it that some of the controls in my user control (text boxes)
    > > maintain their state, yet others (drop downs) do not?
    > >
    > > And I'm not entirely convinced I understand the function of the
    > > ReloadTheControl() method in your example, or how to distinguish one
    > > control from another using ViewState.Add("ControlLoaded", true), since
    > > there many be many of these user controls on the page.
    > >
    > > Thanks, Mark
    > >
    > > Karl Seguin [MVP] wrote:
    > >> Just to add to that, you can use Denis Bauer's DynamicControlsPlaceholder
    > >> to
    > >> hepl you do this (free):
    > >> http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
    > >>
    > >> or you can roll the logic urself, which isn't too hard. Normally people
    > >> store the this value in the ViewState:
    > >>
    > >> LoadControl("asdsa.ascx");
    > >> ViewState.Add("ControlLoaded", true);
    > >>
    > >> and then check on postback:
    > >>
    > >> if (Page.IsPostBack AND (bool)ViewState["ControlLoaded"])
    > >> {
    > >> ReloadTheControl();
    > >> }
    > >>
    > >> if you reloadthecontrol during or before onLoad, state should be
    > >> maintained.
    > >>
    > >> Karl
    > >>
    > >> --
    > >> http://www.openmymind.net/
    > >> http://www.codebetter.com/
    > >>
    > >>
    > >> "Kevin Spencer" <> wrote in message
    > >> news:...
    > >> > Since the Control is dynamically added, it is not referenced in the
    > >> > class
    > >> > definition. Since the Page class must be rebuilt from scratch with each
    > >> > PostBack, the dynamically-created Control must be manually restored
    > >> > with
    > >> > each PostBack.
    > >> >
    > >> > --
    > >> > HTH,
    > >> >
    > >> > Kevin Spencer
    > >> > Microsoft MVP
    > >> > Chicken Salad Surgery
    > >> >
    > >> > Orange you bland I stopped splaying bananas?
    > >> >
    > >> >
    > >> > <> wrote in message
    > >> > news:...
    > >> >> I've asked this question before, but still haven't solved it, so am
    > >> >> asking again.
    > >> >>
    > >> >> I am programmatically adding a user control to the page in response to
    > >> >> a button click. The user control consists of three dropdowns and seven
    > >> >> text boxes.
    > >> >>
    > >> >> When the button is clicked, I add another control to the page in Click
    > >> >> event of the button and populate the three dropdowns. The text boxes
    > >> >> are to be populated by the user.
    > >> >>
    > >> >> All ok so far, but when I click the button again, since it is a
    > >> >> postback, I do not rebind the data to the dropdowns and their options
    > >> >> disappear, although the seven text boxes maintain their state.
    > >> >>
    > >> >> Can anyone explain why this might be?
    > >> >>
    > >> >> Ta, Mark
    > >> >>
    > >> >
    > >> >

    > >
     
    , Aug 16, 2006
    #7
  8. In this case, you'll want to make sure viewstate is disabled for them.
    Otherwise you'll get the double hit of storing everything in the viewstate
    and rebinding.

    Karl

    --
    http://www.openmymind.net/
    http://www.codebetter.com/


    <> wrote in message
    news:...
    > Ok, thanks a lot for taking the time Karl.
    >
    > I have found that by binding the data to the drop downs, even when
    > posting back, the selected values of the drop downs is maintained,
    > which is exactly the behaviour I want.
    >
    > Thanks for your help!
    >
    > Mark
    >
    > Karl Seguin [MVP] wrote:
    >> Mark:
    >> Textboxes are very unique in their ability to retain their state. They'll
    >> retain their state via Request.Form, rather than the ViewState like most
    >> (all?) other controls - that's because they are single valued. Don't be
    >> thrown off by this unique behaviour :)
    >>
    >>
    >>
    >> ReloadTheControl() was just a stub function...all it would do is
    >> something
    >> like:
    >>
    >> Blah.Controls.Add(Page.LoadControl("adsadsa.ascx"))..
    >>
    >>
    >> if you have multiple controls, you'll need to store a more complicated
    >> object in the viewstate...such as an arraylist of strings...
    >>
    >>
    >> public void AddDynamicControl(string path)
    >> {
    >> ArrayList controlsToReload = (ArrayList)ViewState["dynamicControls"];
    >> if (controlsToReload == null)
    >> {
    >> controlsToReload = new ArrayList();
    >> ViewState.Add("dynamicControls", controlsToReload);
    >> }
    >> controlsToReload .Add(Path);
    >> }
    >>
    >> so whever you dynamically add a new control, you call AddDynamicControl
    >>
    >> LoadControl("whatever.ascx");
    >> AddDynamicControl("whatever.ascx");
    >>
    >>
    >> Then on PostBack, you can reload all the controls:
    >>
    >> public vod ReloadDynamicControls()
    >> {
    >> ArrayList controslToReload = (ArrayList)ViewState["dynamicControls"];
    >> if (controslToReload ! = null)
    >> {
    >> foreach (string path in controslToReload)
    >> {
    >> Page.LoadControl(path);
    >> }
    >> }
    >> }
    >>
    >> Anyways, all of this is just some code off hte top of my head, you'll
    >> likely
    >> need to fx it up a bit/a lot :)
    >>
    >> Karl
    >>
    >> --
    >> http://www.openmymind.net/
    >> http://www.fuelindustries.com/
    >>
    >>
    >> <> wrote in message
    >> news:...
    >> > Karl
    >> >
    >> > Would you mind explaining this in a little more detail? I'm reluctant
    >> > to use a third-party component because I want to know what the problem
    >> > is for myself so I can increase my understanding.
    >> >
    >> > Why is it that some of the controls in my user control (text boxes)
    >> > maintain their state, yet others (drop downs) do not?
    >> >
    >> > And I'm not entirely convinced I understand the function of the
    >> > ReloadTheControl() method in your example, or how to distinguish one
    >> > control from another using ViewState.Add("ControlLoaded", true), since
    >> > there many be many of these user controls on the page.
    >> >
    >> > Thanks, Mark
    >> >
    >> > Karl Seguin [MVP] wrote:
    >> >> Just to add to that, you can use Denis Bauer's
    >> >> DynamicControlsPlaceholder
    >> >> to
    >> >> hepl you do this (free):
    >> >> http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
    >> >>
    >> >> or you can roll the logic urself, which isn't too hard. Normally
    >> >> people
    >> >> store the this value in the ViewState:
    >> >>
    >> >> LoadControl("asdsa.ascx");
    >> >> ViewState.Add("ControlLoaded", true);
    >> >>
    >> >> and then check on postback:
    >> >>
    >> >> if (Page.IsPostBack AND (bool)ViewState["ControlLoaded"])
    >> >> {
    >> >> ReloadTheControl();
    >> >> }
    >> >>
    >> >> if you reloadthecontrol during or before onLoad, state should be
    >> >> maintained.
    >> >>
    >> >> Karl
    >> >>
    >> >> --
    >> >> http://www.openmymind.net/
    >> >> http://www.codebetter.com/
    >> >>
    >> >>
    >> >> "Kevin Spencer" <> wrote in message
    >> >> news:...
    >> >> > Since the Control is dynamically added, it is not referenced in the
    >> >> > class
    >> >> > definition. Since the Page class must be rebuilt from scratch with
    >> >> > each
    >> >> > PostBack, the dynamically-created Control must be manually restored
    >> >> > with
    >> >> > each PostBack.
    >> >> >
    >> >> > --
    >> >> > HTH,
    >> >> >
    >> >> > Kevin Spencer
    >> >> > Microsoft MVP
    >> >> > Chicken Salad Surgery
    >> >> >
    >> >> > Orange you bland I stopped splaying bananas?
    >> >> >
    >> >> >
    >> >> > <> wrote in message
    >> >> > news:...
    >> >> >> I've asked this question before, but still haven't solved it, so am
    >> >> >> asking again.
    >> >> >>
    >> >> >> I am programmatically adding a user control to the page in response
    >> >> >> to
    >> >> >> a button click. The user control consists of three dropdowns and
    >> >> >> seven
    >> >> >> text boxes.
    >> >> >>
    >> >> >> When the button is clicked, I add another control to the page in
    >> >> >> Click
    >> >> >> event of the button and populate the three dropdowns. The text
    >> >> >> boxes
    >> >> >> are to be populated by the user.
    >> >> >>
    >> >> >> All ok so far, but when I click the button again, since it is a
    >> >> >> postback, I do not rebind the data to the dropdowns and their
    >> >> >> options
    >> >> >> disappear, although the seven text boxes maintain their state.
    >> >> >>
    >> >> >> Can anyone explain why this might be?
    >> >> >>
    >> >> >> Ta, Mark
    >> >> >>
    >> >> >
    >> >> >
    >> >

    >
     
    Karl Seguin [MVP], Aug 16, 2006
    #8
  9. Guest

    Good point! Never thought of that. Perhaps that's why the pages are
    often approaching 1MB in size. I'm expecting a visit from the network
    administrator sometime soon...

    Karl Seguin [MVP] wrote:
    > In this case, you'll want to make sure viewstate is disabled for them.
    > Otherwise you'll get the double hit of storing everything in the viewstate
    > and rebinding.
    >
    > Karl
    >
    > --
    > http://www.openmymind.net/
    > http://www.codebetter.com/
    >
    >
    > <> wrote in message
    > news:...
    > > Ok, thanks a lot for taking the time Karl.
    > >
    > > I have found that by binding the data to the drop downs, even when
    > > posting back, the selected values of the drop downs is maintained,
    > > which is exactly the behaviour I want.
    > >
    > > Thanks for your help!
    > >
    > > Mark
    > >
    > > Karl Seguin [MVP] wrote:
    > >> Mark:
    > >> Textboxes are very unique in their ability to retain their state. They'll
    > >> retain their state via Request.Form, rather than the ViewState like most
    > >> (all?) other controls - that's because they are single valued. Don't be
    > >> thrown off by this unique behaviour :)
    > >>
    > >>
    > >>
    > >> ReloadTheControl() was just a stub function...all it would do is
    > >> something
    > >> like:
    > >>
    > >> Blah.Controls.Add(Page.LoadControl("adsadsa.ascx"))..
    > >>
    > >>
    > >> if you have multiple controls, you'll need to store a more complicated
    > >> object in the viewstate...such as an arraylist of strings...
    > >>
    > >>
    > >> public void AddDynamicControl(string path)
    > >> {
    > >> ArrayList controlsToReload = (ArrayList)ViewState["dynamicControls"];
    > >> if (controlsToReload == null)
    > >> {
    > >> controlsToReload = new ArrayList();
    > >> ViewState.Add("dynamicControls", controlsToReload);
    > >> }
    > >> controlsToReload .Add(Path);
    > >> }
    > >>
    > >> so whever you dynamically add a new control, you call AddDynamicControl
    > >>
    > >> LoadControl("whatever.ascx");
    > >> AddDynamicControl("whatever.ascx");
    > >>
    > >>
    > >> Then on PostBack, you can reload all the controls:
    > >>
    > >> public vod ReloadDynamicControls()
    > >> {
    > >> ArrayList controslToReload = (ArrayList)ViewState["dynamicControls"];
    > >> if (controslToReload ! = null)
    > >> {
    > >> foreach (string path in controslToReload)
    > >> {
    > >> Page.LoadControl(path);
    > >> }
    > >> }
    > >> }
    > >>
    > >> Anyways, all of this is just some code off hte top of my head, you'll
    > >> likely
    > >> need to fx it up a bit/a lot :)
    > >>
    > >> Karl
    > >>
    > >> --
    > >> http://www.openmymind.net/
    > >> http://www.fuelindustries.com/
    > >>
    > >>
    > >> <> wrote in message
    > >> news:...
    > >> > Karl
    > >> >
    > >> > Would you mind explaining this in a little more detail? I'm reluctant
    > >> > to use a third-party component because I want to know what the problem
    > >> > is for myself so I can increase my understanding.
    > >> >
    > >> > Why is it that some of the controls in my user control (text boxes)
    > >> > maintain their state, yet others (drop downs) do not?
    > >> >
    > >> > And I'm not entirely convinced I understand the function of the
    > >> > ReloadTheControl() method in your example, or how to distinguish one
    > >> > control from another using ViewState.Add("ControlLoaded", true), since
    > >> > there many be many of these user controls on the page.
    > >> >
    > >> > Thanks, Mark
    > >> >
    > >> > Karl Seguin [MVP] wrote:
    > >> >> Just to add to that, you can use Denis Bauer's
    > >> >> DynamicControlsPlaceholder
    > >> >> to
    > >> >> hepl you do this (free):
    > >> >> http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
    > >> >>
    > >> >> or you can roll the logic urself, which isn't too hard. Normally
    > >> >> people
    > >> >> store the this value in the ViewState:
    > >> >>
    > >> >> LoadControl("asdsa.ascx");
    > >> >> ViewState.Add("ControlLoaded", true);
    > >> >>
    > >> >> and then check on postback:
    > >> >>
    > >> >> if (Page.IsPostBack AND (bool)ViewState["ControlLoaded"])
    > >> >> {
    > >> >> ReloadTheControl();
    > >> >> }
    > >> >>
    > >> >> if you reloadthecontrol during or before onLoad, state should be
    > >> >> maintained.
    > >> >>
    > >> >> Karl
    > >> >>
    > >> >> --
    > >> >> http://www.openmymind.net/
    > >> >> http://www.codebetter.com/
    > >> >>
    > >> >>
    > >> >> "Kevin Spencer" <> wrote in message
    > >> >> news:...
    > >> >> > Since the Control is dynamically added, it is not referenced in the
    > >> >> > class
    > >> >> > definition. Since the Page class must be rebuilt from scratch with
    > >> >> > each
    > >> >> > PostBack, the dynamically-created Control must be manually restored
    > >> >> > with
    > >> >> > each PostBack.
    > >> >> >
    > >> >> > --
    > >> >> > HTH,
    > >> >> >
    > >> >> > Kevin Spencer
    > >> >> > Microsoft MVP
    > >> >> > Chicken Salad Surgery
    > >> >> >
    > >> >> > Orange you bland I stopped splaying bananas?
    > >> >> >
    > >> >> >
    > >> >> > <> wrote in message
    > >> >> > news:...
    > >> >> >> I've asked this question before, but still haven't solved it, so am
    > >> >> >> asking again.
    > >> >> >>
    > >> >> >> I am programmatically adding a user control to the page in response
    > >> >> >> to
    > >> >> >> a button click. The user control consists of three dropdowns and
    > >> >> >> seven
    > >> >> >> text boxes.
    > >> >> >>
    > >> >> >> When the button is clicked, I add another control to the page in
    > >> >> >> Click
    > >> >> >> event of the button and populate the three dropdowns. The text
    > >> >> >> boxes
    > >> >> >> are to be populated by the user.
    > >> >> >>
    > >> >> >> All ok so far, but when I click the button again, since it is a
    > >> >> >> postback, I do not rebind the data to the dropdowns and their
    > >> >> >> options
    > >> >> >> disappear, although the seven text boxes maintain their state.
    > >> >> >>
    > >> >> >> Can anyone explain why this might be?
    > >> >> >>
    > >> >> >> Ta, Mark
    > >> >> >>
    > >> >> >
    > >> >> >
    > >> >

    > >
     
    , Aug 17, 2006
    #9
    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. Vinay Joseph Mathew

    Losing Session State on a State Server

    Vinay Joseph Mathew, Aug 10, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    601
    =?Utf-8?B?c29ubnk=?=
    Sep 16, 2004
  2. Josh
    Replies:
    1
    Views:
    531
    ashish
    Mar 23, 2005
  3. Michael Lang
    Replies:
    12
    Views:
    914
    Michael Lang
    Dec 29, 2006
  4. Replies:
    0
    Views:
    337
  5. KMILO
    Replies:
    0
    Views:
    135
    KMILO
    Jan 15, 2007
Loading...

Share This Page