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