By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,796 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,796 IT Pros & Developers. It's quick & easy.

Urgent : Viewstate and dynamic controls !ONCE AND FOR ALL PLEASE!

P: n/a
Hi all,

I need finality on this once and for all please.

I have a main page which contains a couple of placeholders and within these
placeholders, depending on what the user presses, I load different user
controls. This loading of user controls is done within the code-behind of
the main page. The problem is that before loading a user control in place of
another, I need to do a Controls.Add then a Controls.Remove so the viewstate
is loaded then cleared so I can continue.

Now I have posted this message in different fomrs on this NG before but
never had viable solutions to my problem.

Surely people out there are using dynamic user controls (and simply placing
20 user controls inside an area on a page and setting the visible property
isn't practical).

Please someone, provide me with an explanation of how to do this correctly.
Providing pointers to some web sites that explain this (and not just
viewstate) would really help me. I'm not impartial to reading, believe me. I
really need an understaing of this once and for all - again, please.

Regards
John.
Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
> I need finality on this once and for all please.

'finality' and 'newsgroups' rarely go together. ;o)
Please someone, provide me with an explanation of how to do this correctly. Providing pointers to some web sites that explain this (and not just
viewstate) would really help me. I'm not impartial to reading, believe me. I really need an understaing of this once and for all - again, please.


I'm not sure if I can answer your question...but I guess I'm not completely
sure what your question is. Are you asking how to clear viewstate between
page loads?

-Darrel
Nov 19 '05 #2

P: n/a
Hi Darrel,

No. I'm asking for the correct way/method to use in my programmes to
dynamically add and remove user controls from within a placeholder.

Example:
I have a page with a placeholder on it.
The first time the page posts to the server, I load usercontrol A.

The second time the page posts back, I want to load usercontrol B but if I
do this immediately, I receive an error which complaining aboutnnot being
able to reload viewstate in usercontrol A because the sequence in which
controls are added has changed since the prvious post. At this stage, I don'
even want to use any information from usercontrol A, I just want to load
usercontrol B and send to the client.

What is the correct method to do this for multiple usercontrols?

"darrel" <no*****@hotmail.com> wrote in message
news:uM**************@TK2MSFTNGP10.phx.gbl...
I need finality on this once and for all please.


'finality' and 'newsgroups' rarely go together. ;o)
Please someone, provide me with an explanation of how to do this

correctly.
Providing pointers to some web sites that explain this (and not just
viewstate) would really help me. I'm not impartial to reading, believe
me.

I
really need an understaing of this once and for all - again, please.


I'm not sure if I can answer your question...but I guess I'm not
completely
sure what your question is. Are you asking how to clear viewstate between
page loads?

-Darrel

Nov 19 '05 #3

P: n/a
> No. I'm asking for the correct way/method to use in my programmes to
dynamically add and remove user controls from within a placeholder.
Ah...
The second time the page posts back, I want to load usercontrol B but if I
do this immediately, I receive an error which complaining aboutnnot being
able to reload viewstate in usercontrol A because the sequence in which
controls are added has changed since the prvious post.


Hmm...probably won't be much help there. I've only done dynamically loading
of one control at a time. I'm curious as to the answer myself, though...

Just a guess, but would be something like this work?:

on event that triggers postback...
unload control a
load control b

-Darrel
Nov 19 '05 #4

P: n/a
So create the control the first time in the button click, and "remember"
that you created that control. Then it's up to you to recreate all the right
controls on the page upon every postback. This is typically done in Page_Init,
Page_Load or by overriding CreateChildControls.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi all,

I need finality on this once and for all please.

I have a main page which contains a couple of placeholders and within
these placeholders, depending on what the user presses, I load
different user controls. This loading of user controls is done within
the code-behind of the main page. The problem is that before loading a
user control in place of another, I need to do a Controls.Add then a
Controls.Remove so the viewstate is loaded then cleared so I can
continue.

Now I have posted this message in different fomrs on this NG before
but never had viable solutions to my problem.

Surely people out there are using dynamic user controls (and simply
placing 20 user controls inside an area on a page and setting the
visible property isn't practical).

Please someone, provide me with an explanation of how to do this
correctly. Providing pointers to some web sites that explain this (and
not just viewstate) would really help me. I'm not impartial to
reading, believe me. I really need an understaing of this once and for
all - again, please.

Regards
John.


Nov 19 '05 #5

P: n/a
I'm with you on the unload control A then load control B but the only way I
can see it working is messy:

Firstly, where do you envisige the unloading and loading occurring?

I have it in my main page. Since the main page always fires first, I have a
procedure which checks two variables - say ControlNew and ControlPrevious.
The user clicked on a button which then calls a Javascript function which
changes a hidden field (ControlNew) to a specific value ('ControlB').

Within the Page_Load event of the main page, my procedure is called and this
first checks for a difference between ControlNew and ControlPrevious. If ont
exists, I do a

Dim c As Control = Page.LoadControl(Session("h_UCPrevMain"))

Me.plcMain.Controls.Add(Page.LoadControl(c)

then

Me.plcMain.Controls.Remove(c)

then

Me.plcMain.Controls.Add(Page.LoadControl(Session(" ControlNew ")))

The problem I'm having with this approach is that firstly, it seems I'm
having to use way too much Javascript to get this to work the way I see it
and secondly, I just get the feeling I'm missing somethiing completely
regarding my whoole approach - there HAS to be a better way I'm not seeing.


"darrel" <no*****@hotmail.com> wrote in message
news:Ov**************@TK2MSFTNGP12.phx.gbl...
No. I'm asking for the correct way/method to use in my programmes to
dynamically add and remove user controls from within a placeholder.


Ah...
The second time the page posts back, I want to load usercontrol B but if
I
do this immediately, I receive an error which complaining aboutnnot being
able to reload viewstate in usercontrol A because the sequence in which
controls are added has changed since the prvious post.


Hmm...probably won't be much help there. I've only done dynamically
loading
of one control at a time. I'm curious as to the answer myself, though...

Just a guess, but would be something like this work?:

on event that triggers postback...
unload control a
load control b

-Darrel

Nov 19 '05 #6

P: n/a
Brock,

Please could you perhaps just post a small example showing what you stated
in your previous post?

I've just tried what you suggested by "remembering" to load the page and it
does load but now I've lost my viewstate.

An example would do me a world of good right now. Please.

It's 3 in the morning and I've gotta sleep a bit.
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:48**********************@msnews.microsoft.com ...
So create the control the first time in the button click, and "remember"
that you created that control. Then it's up to you to recreate all the
right controls on the page upon every postback. This is typically done in
Page_Init, Page_Load or by overriding CreateChildControls.
-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi all,

I need finality on this once and for all please.

I have a main page which contains a couple of placeholders and within
these placeholders, depending on what the user presses, I load
different user controls. This loading of user controls is done within
the code-behind of the main page. The problem is that before loading a
user control in place of another, I need to do a Controls.Add then a
Controls.Remove so the viewstate is loaded then cleared so I can
continue.

Now I have posted this message in different fomrs on this NG before
but never had viable solutions to my problem.

Surely people out there are using dynamic user controls (and simply
placing 20 user controls inside an area on a page and setting the
visible property isn't practical).

Please someone, provide me with an explanation of how to do this
correctly. Providing pointers to some web sites that explain this (and
not just viewstate) would really help me. I'm not impartial to
reading, believe me. I really need an understaing of this once and for
all - again, please.

Regards
John.


Nov 19 '05 #7

P: n/a
Hey John: Here's the sample (sry for the delay, I was away for the weekend):

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">
ArrayList DynamicControls
{
get
{
if (ViewState["DynamicControls"] == null)
{
ViewState["DynamicControls"] = new ArrayList();
}
return (ArrayList)ViewState["DynamicControls"];
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
for (int i = 0; i < DynamicControls.Count; i++)
{
string s = (string)DynamicControls[i];
Control c = null;
switch (s)
{
case "t":
c = new TextBox();
break;
case "d":
c = new DropDownList();
break;
case "l":
c = new Label();
break;
}
if (c != null)
{
_ph.Controls.Add(c);
}
}
}
}

protected void _button1_Click(object sender, EventArgs e)
{
TextBox t = new TextBox();
_ph.Controls.Add(t);

DynamicControls.Add("t");
}
protected void _button2_Click(object sender, EventArgs e)
{
DropDownList d = new DropDownList();
_ph.Controls.Add(d);

d.Items.Add("Red");
d.Items.Add("Green");
d.Items.Add("Blue");

DynamicControls.Add("d");
}

protected void _button3_Click(object sender, EventArgs e)
{
Label l = new Label();
_ph.Controls.Add(l);

l.Text = DateTime.Now.ToString();

DynamicControls.Add("l");
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat=server ID=_button1 Text="Add TextBox" OnClick="_button1_Click"
/>
&nbsp;
<asp:Button runat=server ID=_button2 Text="Add DropDownList" OnClick="_button2_Click"
/>
&nbsp;
<asp:Button runat="server" ID="_button3" Text="Add Label" OnClick="_button3_Click"
/>
<br />
<asp:PlaceHolder runat="server" ID="_ph"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
-Brock
DevelopMentor
http://staff.develop.com/ballen
Brock,

Please could you perhaps just post a small example showing what you
stated in your previous post?

I've just tried what you suggested by "remembering" to load the page
and it does load but now I've lost my viewstate.

An example would do me a world of good right now. Please.

It's 3 in the morning and I've gotta sleep a bit.

"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:48**********************@msnews.microsoft.com ...
So create the control the first time in the button click, and
"remember"
that you created that control. Then it's up to you to recreate all
the
right controls on the page upon every postback. This is typically
done in
Page_Init, Page_Load or by overriding CreateChildControls.
-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi all,

I need finality on this once and for all please.

I have a main page which contains a couple of placeholders and
within these placeholders, depending on what the user presses, I
load different user controls. This loading of user controls is done
within the code-behind of the main page. The problem is that before
loading a user control in place of another, I need to do a
Controls.Add then a Controls.Remove so the viewstate is loaded then
cleared so I can continue.

Now I have posted this message in different fomrs on this NG before
but never had viable solutions to my problem.

Surely people out there are using dynamic user controls (and simply
placing 20 user controls inside an area on a page and setting the
visible property isn't practical).

Please someone, provide me with an explanation of how to do this
correctly. Providing pointers to some web sites that explain this
(and not just viewstate) would really help me. I'm not impartial to
reading, believe me. I really need an understaing of this once and
for all - again, please.

Regards
John.


Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.