473,545 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1928
> 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*****@hotmai l.com> wrote in message
news:uM******** ******@TK2MSFTN GP10.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 CreateChildCont rols.

-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.LoadContro l(Session("h_UC PrevMain"))

Me.plcMain.Cont rols.Add(Page.L oadControl(c)

then

Me.plcMain.Cont rols.Remove(c)

then

Me.plcMain.Cont rols.Add(Page.L oadControl(Sess ion("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*****@hotmai l.com> wrote in message
news:Ov******** ******@TK2MSFTN GP12.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 "rememberin g" 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****@NOSPAMd evelop.com> wrote in message
news:48******** **************@ msnews.microsof t.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 CreateChildCont rols.
-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["DynamicControl s"] == null)
{
ViewState["DynamicControl s"] = new ArrayList();
}
return (ArrayList)View State["DynamicControl s"];
}
}

protected void Page_Load(objec t sender, EventArgs e)
{
if (IsPostBack)
{
for (int i = 0; i < DynamicControls .Count; i++)
{
string s = (string)Dynamic Controls[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.Ad d(c);
}
}
}
}

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

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

d.Items.Add("Re d");
d.Items.Add("Gr een");
d.Items.Add("Bl ue");

DynamicControls .Add("d");
}

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

l.Text = DateTime.Now.To String();

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat=server ID=_button1 Text="Add TextBox" OnClick="_butto n1_Click"
/>
&nbsp;
<asp:Button runat=server ID=_button2 Text="Add DropDownList" OnClick="_butto n2_Click"
/>
&nbsp;
<asp:Button runat="server" ID="_button3" Text="Add Label" OnClick="_butto n3_Click"
/>
<br />
<asp:PlaceHolde r 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 "rememberin g" 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****@NOSPAMd evelop.com> wrote in message
news:48******** **************@ msnews.microsof t.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 CreateChildCont rols.
-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
4273
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 I know I need to call a function that will save data but I'm not sure exactly when to call this function. I've tried two ways and both seem to...
3
2633
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 and this causes control B to be created, when this is done the VIEWSTATE is lost for CONTROL B. In the EVENT that causes CONTROL B to be created...
4
6719
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 from all the various explanations/answers. I'm attempting the typical scenario... I create a variable number of controls at runtime based on...
6
2731
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 placholder and the user clicks on a specific control outside of the usercontrol and some Javascript is run where a hidden variable is changed to...
1
1911
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 procedure. The set of controls will not always be the same (based on the selection the user makes in a static ddl). The controls are persisted...
1
4821
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 the table contains a SELECT box populated on the initial load. Every time I postback I'm inserting a column into the table, the dropdown always...
6
2842
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 order for a dynamically created control to persist its state, it needs to be created and loaded into the page control hierarchy before the viewstate...
3
2577
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 problems in the following (rather common, I guess) scenario: The controls are created dynamically. Their number and contents
12
1900
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
2223
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 dynamic controls. Our application was having a problem very similar to the one described here: ...
0
7669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7926
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7439
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.