Programmatically added controls losing state

M

mark.norgate

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
 
K

Kevin Spencer

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?
 
K

Karl Seguin [MVP]

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
 
M

mark.norgate

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
 
M

mark.norgate

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
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 said:
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?
 
K

Karl Seguin [MVP]

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/


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
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 said:
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?


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
 
M

mark.norgate

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
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/


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
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/


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?


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
 
K

Karl Seguin [MVP]

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/


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
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/


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/


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?


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
 
M

mark.norgate

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...
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/


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
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/


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/


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?


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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top