472,364 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Viewstate and Dynmac controls

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 I have to set
ChildControlsCreated to false, if I don't set it the viewstate works but the
control doesn't show, as the createchild controls does not get re-ran.

Below is the a smaller version of the code that I have created to reproduce
the problem test as follows :

Click Get Current Time, it will now show the time
Click do post back, it will still show time in first step, as well as time
of post back, to prove its posted back.
Click Raise Event, it has now lost the viewstate, time has gone blank.
Click Get Current Time on both line, you will see the time get filled in and
it persisted in the viewstate, do a few clicks to prove this.
Now Click Raise Event, view state has been lost again.

Cheers

Steve

Project name should be ViewStateTest

Code : (Add to single class called controls.cs)

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ViewStateTest
{
public class ContainTime : WebControl ,INamingContainer
{
ShowTime showtimeA;
ShowTime showtimeB;

WebControl holdingcontrol;

bool showB;

protected override void LoadViewState(object savedState)
{

base.LoadViewState (savedState);
if (ViewState["ShowB"] == null)
{
showB = false;
}
else
{
showB = (bool) ViewState["ShowB"];
}

}
protected override object SaveViewState()
{
ViewState["ShowB"] = showB;
return base.SaveViewState ();
}

protected override void CreateChildControls()
{
Controls.Clear();
showtimeA = new ShowTime();
showtimeA.Click+=new EventHandler(showtimeA_Click);
Controls.Add(showtimeA);

if (showB )
{
showtimeB = new ShowTime();
Controls.Add(showtimeB);
}
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
showtimeA.RenderControl(writer);
if (showB) showtimeB.RenderControl(writer);
}

public ContainTime()
{

}

private void showtimeA_Click(object sender, EventArgs e)
{
this.showB = !showB;
this.ChildControlsCreated = false; // if I dont do this, the control is
there
// its the above line that makes the viewstate vanish, but I need it to
cause the controls to dynamicly add.
}
}

/// <summary>
/// Summary description for ShowTime.
/// </summary>
public class ShowTime : WebControl,INamingContainer
{
public ShowTime()
{
}

string timestring;
Button abutton;
Button getTimeButton;
Button doRoundTrip;

Label timelabel;

public event EventHandler Click;

protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);
System.Diagnostics.Trace.WriteLine("Items in viewstate " +
ViewState.Count);

if (ViewState["time"] == null)
{
timestring = "Not found";
}
else
{
timestring = (string) ViewState["time"];
}
}
protected override object SaveViewState()
{
ViewState["time"] = timestring;
return base.SaveViewState ();
}

protected override void CreateChildControls()
{
// For this example we could just write the time out in the render, but I
need to mimic the class
// that I am having problems with as much as possable.
Controls.Clear();
timelabel = new Label();
timelabel.Text = timestring;
abutton = new Button();
abutton.Text = "Raise Event";
abutton.Click+=new System.EventHandler(abutton_Click);

getTimeButton = new Button();
getTimeButton.Text = "Get Current Time";
getTimeButton.Click+=new EventHandler(getTimeButton_Click);

doRoundTrip = new Button();
doRoundTrip.Text = "Do Post Back";
doRoundTrip.Click+=new EventHandler(doRoundTrip_Click);

Controls.Add(timelabel);
Controls.Add(abutton);
Controls.Add(getTimeButton);
Controls.Add(doRoundTrip);
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.RenderBeginTag("DIV");
writer.Write(this.UniqueID);
EnsureChildControls();
timelabel.RenderControl(writer);
abutton.RenderControl(writer);
getTimeButton.RenderControl(writer);
writer.Write("Time of post back " + System.DateTime.Now.ToString());
doRoundTrip.RenderControl(writer);
writer.RenderEndTag();
}

private void abutton_Click(object sender, System.EventArgs e)
{
Click(this,e);
}

private void getTimeButton_Click(object sender, EventArgs e)
{
this.timestring = System.DateTime.Now.ToString();
ChildControlsCreated=false;
}

private void doRoundTrip_Click(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine("Round trip");
ChildControlsCreated=false;
}
}
}
CODE BEHIND CODE

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ViewStateTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected ViewStateTest.ContainTime ContainTime1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
ASPX PAGE :
<%@ Register TagPrefix="cc1" Namespace="ViewStateTest"
Assembly="ViewStateTest" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="ViewStateTest.WebForm1" smartNavigation="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<cc1:ContainTime id="ContainTime1" style="Z-INDEX: 101; LEFT: 56px;
POSITION: absolute; TOP: 48px"
runat="server"></cc1:ContainTime>
</form>
</body>
</HTML>



Nov 18 '05 #1
3 2480
Dynamicaly added controls are "lost" on each request. They
do actually save their viewstate to the page but on the
next submit they do not read/load it because they're lost,
they dont exist during LoadViewState because you probably
create them later in the page lifecycle like in an
eventhanlder or in Page_Load.

In order for this to work you have to somehow add your
dynamic controls before ViewState is loaded for example in
Page_Init, but another problem arises from this. Let's say
you create some controls and save in your UserControls
ViewState that these controls are created. How to know
which controls you have created since you can't read your
own ViewState in Page_Init? That means you should probably
override the LoadViewState method. I did something like
this I think(!), it was pretty simple and I think it
worked. I overrided LoadViewState. In LoadViewState I
called base.LoadViewState() checked the ViewState to see
which controls I have created, added/created them again
and called base.LoadViewState() with the child controls
created again :) I'm not sure it's the best solution or
that even it will work properly but I hope this helped you
understand the problem...

Sometimes it is much more simple to keep your controls
static and invisible than bother with this though..

-----Original Message-----
All,

I have a CONTROL that contains 1 control (Control ONE), the 1 control thatit can contain 1 or 2 control (Control A and B).

Control A, raises and event and Control ONE receives this event and thiscauses control B to be created, when this is done the VIEWSTATE is lost forCONTROL B.

In the EVENT that causes CONTROL B to be created I have to setChildControlsCreated to false, if I don't set it the viewstate works but thecontrol doesn't show, as the createchild controls does not get re-ran.
Below is the a smaller version of the code that I have created to reproducethe problem test as follows :

Click Get Current Time, it will now show the time
Click do post back, it will still show time in first step, as well as timeof post back, to prove its posted back.
Click Raise Event, it has now lost the viewstate, time has gone blank.Click Get Current Time on both line, you will see the time get filled in andit persisted in the viewstate, do a few clicks to prove this.Now Click Raise Event, view state has been lost again.

Cheers

Steve

Project name should be ViewStateTest

Code : (Add to single class called controls.cs)

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ViewStateTest
{
public class ContainTime : WebControl ,INamingContainer
{
ShowTime showtimeA;
ShowTime showtimeB;

WebControl holdingcontrol;

bool showB;

protected override void LoadViewState(object savedState)
{

base.LoadViewState (savedState);
if (ViewState["ShowB"] == null)
{
showB = false;
}
else
{
showB = (bool) ViewState["ShowB"];
}

}
protected override object SaveViewState()
{
ViewState["ShowB"] = showB;
return base.SaveViewState ();
}

protected override void CreateChildControls()
{
Controls.Clear();
showtimeA = new ShowTime();
showtimeA.Click+=new EventHandler(showtimeA_Click);
Controls.Add(showtimeA);

if (showB )
{
showtimeB = new ShowTime();
Controls.Add(showtimeB);
}
}
protected override void Render (System.Web.UI.HtmlTextWriter writer) {
showtimeA.RenderControl(writer);
if (showB) showtimeB.RenderControl(writer);
}

public ContainTime()
{

}

private void showtimeA_Click(object sender, EventArgs e)
{
this.showB = !showB;
this.ChildControlsCreated = false; // if I dont do this, the control isthere
// its the above line that makes the viewstate vanish, but I need it tocause the controls to dynamicly add.
}
}

/// <summary>
/// Summary description for ShowTime.
/// </summary>
public class ShowTime : WebControl,INamingContainer
{
public ShowTime()
{
}

string timestring;
Button abutton;
Button getTimeButton;
Button doRoundTrip;

Label timelabel;

public event EventHandler Click;

protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);
System.Diagnostics.Trace.WriteLine("Items in viewstate " +ViewState.Count);

if (ViewState["time"] == null)
{
timestring = "Not found";
}
else
{
timestring = (string) ViewState["time"];
}
}
protected override object SaveViewState()
{
ViewState["time"] = timestring;
return base.SaveViewState ();
}

protected override void CreateChildControls()
{
// For this example we could just write the time out in the render, but Ineed to mimic the class
// that I am having problems with as much as possable.
Controls.Clear();
timelabel = new Label();
timelabel.Text = timestring;
abutton = new Button();
abutton.Text = "Raise Event";
abutton.Click+=new System.EventHandler(abutton_Click);

getTimeButton = new Button();
getTimeButton.Text = "Get Current Time";
getTimeButton.Click+=new EventHandler (getTimeButton_Click);
doRoundTrip = new Button();
doRoundTrip.Text = "Do Post Back";
doRoundTrip.Click+=new EventHandler(doRoundTrip_Click);

Controls.Add(timelabel);
Controls.Add(abutton);
Controls.Add(getTimeButton);
Controls.Add(doRoundTrip);
}
protected override void Render (System.Web.UI.HtmlTextWriter writer) {
writer.RenderBeginTag("DIV");
writer.Write(this.UniqueID);
EnsureChildControls();
timelabel.RenderControl(writer);
abutton.RenderControl(writer);
getTimeButton.RenderControl(writer);
writer.Write("Time of post back " + System.DateTime.Now.ToString()); doRoundTrip.RenderControl(writer);
writer.RenderEndTag();
}

private void abutton_Click(object sender, System.EventArgs e) {
Click(this,e);
}

private void getTimeButton_Click(object sender, EventArgs e) {
this.timestring = System.DateTime.Now.ToString();
ChildControlsCreated=false;
}

private void doRoundTrip_Click(object sender, EventArgs e) {
System.Diagnostics.Trace.WriteLine("Round trip");
ChildControlsCreated=false;
}
}
}
CODE BEHIND CODE

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ViewStateTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected ViewStateTest.ContainTime ContainTime1;

private void Page_Load(object sender, System.EventArgs e) {
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer. //
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
ASPX PAGE :
<%@ Register TagPrefix="cc1" Namespace="ViewStateTest"
Assembly="ViewStateTest" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"Inherits="ViewStateTest.WebForm1" smartNavigation="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<cc1:ContainTime id="ContainTime1" style="Z-INDEX: 101; LEFT: 56px;POSITION: absolute; TOP: 48px"
runat="server"></cc1:ContainTime>
</form>
</body>
</HTML>



.

Nov 18 '05 #2
These are purely done in server controls, they are created in the correct
order ect and the viewstates does work, but .... the viewstats gets zapped
when a control is added.

This is how my controls are laid out (this looks fine in OE with default
font, if it don't look OK cut and paste the NOTEPAD version at the end.

*********************************
* PAGE *
* *
* **************************** *
* * CONTROL ONE * *
* * * *
* * **************** * *
* * * CONTROL A * * *
* * * [BUTTON] * * *
* * **************** * *
* * * *
* * **************** * *
* * * CONTROL B * * *
* * * [BUTTON] * * *
* * **************** * *
* **************************** *
*********************************

Control B is not created at the start, I click the button on A, CONTROL A
raises an event, CONTROL process the event and stores a flag in the
viewstate to tell CONTROL ONE to create a CONTROL B (this flag keeps its
viewstate ok) , and it creates control B, but control A loses its viewstate
(ONLY CONTROL A) , i then click the button in control B, this does a post
back on all control keep there viewstate.

A work around is to store the viewstat for CONTROL A and B in CONTROL ONE,
this does work, but is going to be tricky todo in other server controls.

I am starting to think that this is just the way it works, you need to test
the code yourself to see the problem.

NOTEPAD VERSION (cut and paste me into notepad)

---- start cut ----
*********************************
* PAGE *
* *
* *************************** *
* * CONTROL ONE * *
* * * *
* * **************** * *
* * * CONTROL A * * *
* * * [BUTTON] * * *
* * **************** * *
* * * *
* * **************** * *
* * * CONTROL B * * *
* * * [BUTTON] * * *
* * **************** * *
* *************************** *
*********************************
---- end cut ----
"Adrijan Josic" <ad*****@vidpata.hr> wrote in message
news:09****************************@phx.gbl...
Dynamicaly added controls are "lost" on each request. They
do actually save their viewstate to the page but on the
next submit they do not read/load it because they're lost,
they dont exist during LoadViewState because you probably
create them later in the page lifecycle like in an
eventhanlder or in Page_Load.

In order for this to work you have to somehow add your
dynamic controls before ViewState is loaded for example in
Page_Init, but another problem arises from this. Let's say
you create some controls and save in your UserControls
ViewState that these controls are created. How to know
which controls you have created since you can't read your
own ViewState in Page_Init? That means you should probably
override the LoadViewState method. I did something like
this I think(!), it was pretty simple and I think it
worked. I overrided LoadViewState. In LoadViewState I
called base.LoadViewState() checked the ViewState to see
which controls I have created, added/created them again
and called base.LoadViewState() with the child controls
created again :) I'm not sure it's the best solution or
that even it will work properly but I hope this helped you
understand the problem...

Sometimes it is much more simple to keep your controls
static and invisible than bother with this though..

-----Original Message-----
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 I have

to set
ChildControlsCreated to false, if I don't set it the

viewstate works but the
control doesn't show, as the createchild controls does

not get re-ran.

Below is the a smaller version of the code that I have

created to reproduce
the problem test as follows :

Click Get Current Time, it will now show the time
Click do post back, it will still show time in first

step, as well as time
of post back, to prove its posted back.
Click Raise Event, it has now lost the viewstate, time

has gone blank.
Click Get Current Time on both line, you will see the

time get filled in and
it persisted in the viewstate, do a few clicks to prove

this.
Now Click Raise Event, view state has been lost again.

Cheers

Steve

Project name should be ViewStateTest

Code : (Add to single class called controls.cs)

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ViewStateTest
{
public class ContainTime : WebControl ,INamingContainer
{
ShowTime showtimeA;
ShowTime showtimeB;

WebControl holdingcontrol;

bool showB;

protected override void LoadViewState(object savedState)
{

base.LoadViewState (savedState);
if (ViewState["ShowB"] == null)
{
showB = false;
}
else
{
showB = (bool) ViewState["ShowB"];
}

}
protected override object SaveViewState()
{
ViewState["ShowB"] = showB;
return base.SaveViewState ();
}

protected override void CreateChildControls()
{
Controls.Clear();
showtimeA = new ShowTime();
showtimeA.Click+=new EventHandler(showtimeA_Click);
Controls.Add(showtimeA);

if (showB )
{
showtimeB = new ShowTime();
Controls.Add(showtimeB);
}
}
protected override void Render

(System.Web.UI.HtmlTextWriter writer)
{
showtimeA.RenderControl(writer);
if (showB) showtimeB.RenderControl(writer);
}

public ContainTime()
{

}

private void showtimeA_Click(object sender, EventArgs e)
{
this.showB = !showB;
this.ChildControlsCreated = false; // if I dont do

this, the control is
there
// its the above line that makes the viewstate vanish,

but I need it to
cause the controls to dynamicly add.
}
}

/// <summary>
/// Summary description for ShowTime.
/// </summary>
public class ShowTime : WebControl,INamingContainer
{
public ShowTime()
{
}

string timestring;
Button abutton;
Button getTimeButton;
Button doRoundTrip;

Label timelabel;

public event EventHandler Click;

protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);
System.Diagnostics.Trace.WriteLine("Items in

viewstate " +
ViewState.Count);

if (ViewState["time"] == null)
{
timestring = "Not found";
}
else
{
timestring = (string) ViewState["time"];
}
}
protected override object SaveViewState()
{
ViewState["time"] = timestring;
return base.SaveViewState ();
}

protected override void CreateChildControls()
{
// For this example we could just write the time out

in the render, but I
need to mimic the class
// that I am having problems with as much as possable.
Controls.Clear();
timelabel = new Label();
timelabel.Text = timestring;
abutton = new Button();
abutton.Text = "Raise Event";
abutton.Click+=new System.EventHandler(abutton_Click);

getTimeButton = new Button();
getTimeButton.Text = "Get Current Time";
getTimeButton.Click+=new EventHandler

(getTimeButton_Click);

doRoundTrip = new Button();
doRoundTrip.Text = "Do Post Back";
doRoundTrip.Click+=new EventHandler(doRoundTrip_Click);

Controls.Add(timelabel);
Controls.Add(abutton);
Controls.Add(getTimeButton);
Controls.Add(doRoundTrip);
}
protected override void Render

(System.Web.UI.HtmlTextWriter writer)
{
writer.RenderBeginTag("DIV");
writer.Write(this.UniqueID);
EnsureChildControls();
timelabel.RenderControl(writer);
abutton.RenderControl(writer);
getTimeButton.RenderControl(writer);
writer.Write("Time of post back " +

System.DateTime.Now.ToString());
doRoundTrip.RenderControl(writer);
writer.RenderEndTag();
}

private void abutton_Click(object sender,

System.EventArgs e)
{
Click(this,e);
}

private void getTimeButton_Click(object sender,

EventArgs e)
{
this.timestring = System.DateTime.Now.ToString();
ChildControlsCreated=false;
}

private void doRoundTrip_Click(object sender, EventArgs

e)
{
System.Diagnostics.Trace.WriteLine("Round trip");
ChildControlsCreated=false;
}
}
}
CODE BEHIND CODE

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ViewStateTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected ViewStateTest.ContainTime ContainTime1;

private void Page_Load(object sender, System.EventArgs

e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web

Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
ASPX PAGE :
<%@ Register TagPrefix="cc1" Namespace="ViewStateTest"
Assembly="ViewStateTest" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"

AutoEventWireup="false"
Inherits="ViewStateTest.WebForm1" smartNavigation="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0

Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual

Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript"

content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<cc1:ContainTime id="ContainTime1" style="Z-INDEX:

101; LEFT: 56px;
POSITION: absolute; TOP: 48px"
runat="server"></cc1:ContainTime>
</form>
</body>
</HTML>



.

Nov 18 '05 #3
I fixed this as follows (note new BASE CLASS), comments welcome:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ViewStateTest
{
public class myControl : WebControl
{
internal object mySaveViewState()
{
return this.SaveViewState ();
}
internal void myLoadViewState(object savedState)
{
this.LoadViewState (savedState);
}
internal void myTrackViewState()
{
this.TrackViewState();
}
}

public class ContainTime : myControl,INamingContainer
{

showtime showtimeA=new showtime();
showtime showtimeB=new showtime();

System.Web.UI.WebControls.Calendar calA;
System.Web.UI.WebControls.Calendar calB;

WebControl holdingcontrol;

bool showB;

protected override void LoadViewState(object savedState)
{
if (savedState!=null)
{
object[] states = (object[]) savedState;

if(states[0] != null)
base.LoadViewState(states[0]);
if(states[1] != null)
showtimeA.myLoadViewState(states[1]);
if(states[2] != null)
showtimeB.myLoadViewState(states[2]);
}

/* Load Custom View State here */
object o = ViewState["ShowB"] ;
if (o== null)
showB = false;
else
showB = (bool) o;

}
protected override object SaveViewState()
{

ViewState["ShowB"] = showB;

object[] states = new object [3];
states[0] = base.SaveViewState();
states[1] = (showtimeA== null ? null : showtimeA.mySaveViewState());
states[2] = (showtimeB== null ? null : showtimeB.mySaveViewState());
for(int i=0; i < states.Length; i++)
{
if(states[i]!=null)
return states;
}
return null;
}
protected override void CreateChildControls()
{
Controls.Clear();
showtimeA.ID = "SA";
showtimeB.ID = "SB";

showtimeA.Click+=new EventHandler(showtimeA_Click);
Controls.Add(showtimeA);
calA = new Calendar();
Controls.Add(calA);

// if (showB )
{
calB = new Calendar();
Controls.Add(showtimeB);
Controls.Add(calB);
}
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
showtimeA.RenderControl(writer);
calA.RenderControl(writer);
if (showB) showtimeB.RenderControl(writer);
if (showB) calB.RenderControl(writer);
}
protected override void TrackViewState()
{
base.TrackViewState ();
}

public ContainTime()
{
showtimeA=new showtime();
showtimeB=new showtime();

}

private void showtimeA_Click(object sender, EventArgs e)
{
this.showB = !showB;
this.ChildControlsCreated = false;
}
}

/// <summary>
/// Summary description for showtime.
/// </summary>
public class showtime : myControl,INamingContainer
{
public showtime()
{
}

string timestring;
Button abutton;
Button getTimeButton;
Button doRoundTrip;

Label timelabel;

public event EventHandler Click;

protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);
System.Diagnostics.Trace.WriteLine("Items in viewstate " +
ViewState.Count);

if (ViewState["time"] == null)
{
timestring = "Not found";
}
else
{
timestring = (string) ViewState["time"];
}
}
protected override object SaveViewState()
{
ViewState["time"] = timestring;
return base.SaveViewState ();
}

protected override void CreateChildControls()
{
// For this example we could just write the time out in the render, but I
need to mimic the class
// that I am having problems with as much as possable.
Controls.Clear();
timelabel = new Label();
timelabel.Text = timestring;
abutton = new Button();
abutton.Text = "Raise Event";
abutton.Click+=new System.EventHandler(abutton_Click);

getTimeButton = new Button();
getTimeButton.Text = "Get Current Time";
getTimeButton.Click+=new EventHandler(getTimeButton_Click);

doRoundTrip = new Button();
doRoundTrip.Text = "Do Post Back";
doRoundTrip.Click+=new EventHandler(doRoundTrip_Click);

Controls.Add(timelabel);
Controls.Add(abutton);
Controls.Add(getTimeButton);
Controls.Add(doRoundTrip);
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.RenderBeginTag("DIV");
writer.Write(this.UniqueID);
EnsureChildControls();
timelabel.RenderControl(writer);
abutton.RenderControl(writer);
getTimeButton.RenderControl(writer);
writer.Write("Time of post back " + System.DateTime.Now.ToString());
doRoundTrip.RenderControl(writer);
writer.RenderEndTag();
}

private void abutton_Click(object sender, System.EventArgs e)
{
base.TrackViewState ();

Click(this,e);
}

private void getTimeButton_Click(object sender, EventArgs e)
{
this.timestring = System.DateTime.Now.ToString();
ChildControlsCreated=false;
}

private void doRoundTrip_Click(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine("Round trip");
ChildControlsCreated=false;
}
}
}

"Steve Drake" <Steve@_NOSPAM_Drakey.co.uk> wrote in message
news:e1**************@TK2MSFTNGP09.phx.gbl...
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 I have to set
ChildControlsCreated to false, if I don't set it the viewstate works but the control doesn't show, as the createchild controls does not get re-ran.

Below is the a smaller version of the code that I have created to reproduce the problem test as follows :

Click Get Current Time, it will now show the time
Click do post back, it will still show time in first step, as well as time
of post back, to prove its posted back.
Click Raise Event, it has now lost the viewstate, time has gone blank.
Click Get Current Time on both line, you will see the time get filled in and it persisted in the viewstate, do a few clicks to prove this.
Now Click Raise Event, view state has been lost again.

Cheers

Steve

Project name should be ViewStateTest

Code : (Add to single class called controls.cs)

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ViewStateTest
{
public class ContainTime : WebControl ,INamingContainer
{
ShowTime showtimeA;
ShowTime showtimeB;

WebControl holdingcontrol;

bool showB;

protected override void LoadViewState(object savedState)
{

base.LoadViewState (savedState);
if (ViewState["ShowB"] == null)
{
showB = false;
}
else
{
showB = (bool) ViewState["ShowB"];
}

}
protected override object SaveViewState()
{
ViewState["ShowB"] = showB;
return base.SaveViewState ();
}

protected override void CreateChildControls()
{
Controls.Clear();
showtimeA = new ShowTime();
showtimeA.Click+=new EventHandler(showtimeA_Click);
Controls.Add(showtimeA);

if (showB )
{
showtimeB = new ShowTime();
Controls.Add(showtimeB);
}
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
showtimeA.RenderControl(writer);
if (showB) showtimeB.RenderControl(writer);
}

public ContainTime()
{

}

private void showtimeA_Click(object sender, EventArgs e)
{
this.showB = !showB;
this.ChildControlsCreated = false; // if I dont do this, the control is
there
// its the above line that makes the viewstate vanish, but I need it to
cause the controls to dynamicly add.
}
}

/// <summary>
/// Summary description for ShowTime.
/// </summary>
public class ShowTime : WebControl,INamingContainer
{
public ShowTime()
{
}

string timestring;
Button abutton;
Button getTimeButton;
Button doRoundTrip;

Label timelabel;

public event EventHandler Click;

protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);
System.Diagnostics.Trace.WriteLine("Items in viewstate " +
ViewState.Count);

if (ViewState["time"] == null)
{
timestring = "Not found";
}
else
{
timestring = (string) ViewState["time"];
}
}
protected override object SaveViewState()
{
ViewState["time"] = timestring;
return base.SaveViewState ();
}

protected override void CreateChildControls()
{
// For this example we could just write the time out in the render, but I need to mimic the class
// that I am having problems with as much as possable.
Controls.Clear();
timelabel = new Label();
timelabel.Text = timestring;
abutton = new Button();
abutton.Text = "Raise Event";
abutton.Click+=new System.EventHandler(abutton_Click);

getTimeButton = new Button();
getTimeButton.Text = "Get Current Time";
getTimeButton.Click+=new EventHandler(getTimeButton_Click);

doRoundTrip = new Button();
doRoundTrip.Text = "Do Post Back";
doRoundTrip.Click+=new EventHandler(doRoundTrip_Click);

Controls.Add(timelabel);
Controls.Add(abutton);
Controls.Add(getTimeButton);
Controls.Add(doRoundTrip);
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.RenderBeginTag("DIV");
writer.Write(this.UniqueID);
EnsureChildControls();
timelabel.RenderControl(writer);
abutton.RenderControl(writer);
getTimeButton.RenderControl(writer);
writer.Write("Time of post back " + System.DateTime.Now.ToString());
doRoundTrip.RenderControl(writer);
writer.RenderEndTag();
}

private void abutton_Click(object sender, System.EventArgs e)
{
Click(this,e);
}

private void getTimeButton_Click(object sender, EventArgs e)
{
this.timestring = System.DateTime.Now.ToString();
ChildControlsCreated=false;
}

private void doRoundTrip_Click(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine("Round trip");
ChildControlsCreated=false;
}
}
}
CODE BEHIND CODE

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ViewStateTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected ViewStateTest.ContainTime ContainTime1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
ASPX PAGE :
<%@ Register TagPrefix="cc1" Namespace="ViewStateTest"
Assembly="ViewStateTest" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ViewStateTest.WebForm1" smartNavigation="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<cc1:ContainTime id="ContainTime1" style="Z-INDEX: 101; LEFT: 56px;
POSITION: absolute; TOP: 48px"
runat="server"></cc1:ContainTime>
</form>
</body>
</HTML>


Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: theo | last post by:
Hi... I wish to extract the text content of an Xml file and assign it to DropDownList controls at runtime.I can get the Xml file text content into the DropDownList controls (Ex...if 5 Xml text...
9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
10
by: neo | last post by:
hi, I am studying ASP.NET and have few questions - 1) The session ID and values of controls is stored in VIEWSTATE variable. So now when we put EnableViewState="false" in Page directive and...
8
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
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: hitendra15 | last post by:
Hi I have created web user control which has Repeater control and Linkbutton in ItemTemplate of repeater control, following is the code for this control On first load it runs fine but when...
1
by: Christophe Peillet | last post by:
I have a CompositeControl with two types of properties: 1.) Mapped Properties that map directly to a child control's properties (ex.: this.TextboxText = m_txt.Text). These properties are handled...
6
by: Peter Zolja | last post by:
Hi, I'm building a webcontrol that contains a dynamic list of other controls. My problem is that when I add or remove an item the synchronization between the ViewState and the Controls...
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
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.