473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
7 1913
> 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
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
> 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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John | last post by:
Hi all, This really is quite an urgent matter. I have a page with multiple, dynamically-loaded user controls and when a user clicks on a button, the whole form is submitted. Now at this stage...
3
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event...
4
by: Chuck Ritzke | last post by:
Hi, I've searched the newsgroup and other sources to understand how to handle runtime controls and see I'm not the only one who's confused, but I'm still not quite sure of the best way to handle...
6
by: John | last post by:
Hi all, I am using dynamic user controls within my web app and these controls are loaded into placeholders via the LoadControl method. My problem is this: I have usercontrolA loaded into a...
1
by: pbb | last post by:
I'm creating a set of dynamic controls on a webpage by calling my BuildControls sub in the Page_Init sub. I recreate the controls by calling the BuildControls sub in the LoadViewState override...
1
by: russ | last post by:
Hi all, Here's a problem I'm having with a dynamic table. Following the guidelines here (http://www.codeproject.com/aspnet/dynamiccontrols.asp), which make perfect sense. The problem is that...
6
by: hlubocky | last post by:
I thought I had a good grasp of the problem related to dynamically creating controls, but it appears that as my application grew in complexity, the problem has resurfaced. As I understand it, in...
3
by: HP | last post by:
Hi there The problem of dynamically created controls vs viewstate is widely known one. To access values of controls they have to be recreated on Page_Load. Unfortunately it causes many...
12
by: Nick C | last post by:
Hi How can i reduce the viewstate for my asp.net application. It is getting very large now. What is a good solution? thanks N
0
by: Scott Roberts | last post by:
I always thought that the viewstate "keys" included the control ID. As long as the control IDs were unique, there shouldn't be any conflicts. Well, it appears that that may not be the case with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.