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

J

John

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

darrel

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
 
J

John

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

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

Brock Allen

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

John

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

John

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

Brock Allen

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;
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>
 
Joined
Dec 1, 2008
Messages
1
Reaction score
0
Similar issue

I know this was posted a long time ago. I am having a similar problem today and have solved most of you posted except for one glitch.
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top