473,385 Members | 2,004 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,385 software developers and data experts.

LoadViewState not firing when Control added with AddAt

Does anyone know of an issue with a control's viewstate not persisting when dynamically adding custom controls to a web form using AddAt?

If a control is added to an HTML form directly using the HtmlForm's AddAt method (per the below code segment), then LoadViewState is never fired for the control, even if SaveViewState, which is fired, saves to the control's ViewState. However, if Add is used instead of AddAt, LoadViewState is called normally.

If anyone can offer any insight as to why this behavior occurs, I'd be grateful.

Thanks.

--
(The below code segment demonstrates the issue.)

namespace N
{
public class Demo : System.Web.UI.Page
{
protected HtmlForm Form;
private void Page_Load(object sender, System.EventArgs e)
{
CC s = new CC();
s.ID="sdf";
Form1.Controls.AddAt(0, s); // if Form1.Controls.Add(s), control handles state properly
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
public class CC : Control
{
protected override void LoadViewState(object savedState) // Never Called
{
ViewState["x"] = new object();
this.Controls.Add(new LiteralControl("Load View State"));
base.LoadViewState (savedState);
}
protected override object SaveViewState()
{
ViewState["x"] = new object();
this.Controls.Add(new LiteralControl("Save View State Called"));
return base.SaveViewState ();
}
}
}
Nov 18 '05 #1
2 3455
"Devin Fensterheim" <Devin Fe*********@discussions.microsoft.com> wrote in
message news:28**********************************@microsof t.com...
Does anyone know of an issue with a control's viewstate not persisting when dynamically adding custom controls to a web form using AddAt?
If a control is added to an HTML form directly using the HtmlForm's AddAt method (per the below code segment), then LoadViewState is never fired for
the control, even if SaveViewState, which is fired, saves to the control's
ViewState. However, if Add is used instead of AddAt, LoadViewState is
called normally.
If anyone can offer any insight as to why this behavior occurs, I'd be

grateful.

I know that ASP.NET keeps track of things by the control's order within the
Controls collection. I know, for instance, that you have to add dynamic
controls in the same order on each postback. I have no reason to believe
that ASP.NET can account for cases where the order changes.
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #2

Assuming you are running this code in an ASPX page, there are likely some LiteralControls (or other controls) that are in the child control collection of the Form1 control. When you use AddAt you are changing the order in which the controls are "created" and thus the ViewState can't be loaded in the Page_Load (it tries to catch up, but it can't since you manually changed the control order).

You can use the AddAt in the OnInit though, since "sdf" will come before the LiteralControls are added; or, if you added the "sdf" to a PlaceHolder things would work out.

Scott
"Devin Fensterheim" <Devin Fe*********@discussions.microsoft.com> wrote in message news:28**********************************@microsof t.com...
Does anyone know of an issue with a control's viewstate not persisting when dynamically adding custom controls to a web form using AddAt?

If a control is added to an HTML form directly using the HtmlForm's AddAt method (per the below code segment), then LoadViewState is never fired for the control, even if SaveViewState, which is fired, saves to the control's ViewState. However, if Add is used instead of AddAt, LoadViewState is called normally.

If anyone can offer any insight as to why this behavior occurs, I'd be grateful.

Thanks.

--
(The below code segment demonstrates the issue.)

namespace N
{
public class Demo : System.Web.UI.Page
{
protected HtmlForm Form;
private void Page_Load(object sender, System.EventArgs e)
{
CC s = new CC();
s.ID="sdf";
Form1.Controls.AddAt(0, s); // if Form1.Controls.Add(s), control handles state properly
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
public class CC : Control
{
protected override void LoadViewState(object savedState) // Never Called
{
ViewState["x"] = new object();
this.Controls.Add(new LiteralControl("Load View State"));
base.LoadViewState (savedState);
}
protected override object SaveViewState()
{
ViewState["x"] = new object();
this.Controls.Add(new LiteralControl("Save View State Called"));
return base.SaveViewState ();
}
}
}
Nov 18 '05 #3

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

Similar topics

2
by: Pete | last post by:
I have some funky form/event behavior. Access 97. Split frontend/backend, using Access security. I have the same behavior (or lack of behavior) for the pag_Click() event of two separate pages...
0
by: | last post by:
hi, Im trying to use below code in three.ascx which is pasted on two.ascx page and two.ascx is pasted on one.aspx page. But "LoadViewState" gives exceptions mentioned below protected override...
0
by: Showjumper | last post by:
Hi, I have implemented the istatemanager interface in control so that i can retrieve values from the viewstate and then update my control. The control is an image gallery and i want it to react to...
2
by: SSW | last post by:
Hi: In aspx.cs page i had written the code below. I have a button control and only create TextBox Control dynamically. LoadViewState is Never called. But when we put datagrid/DataList it...
7
by: Sky | last post by:
What I have currently: I have a user control called mod_container.aspx that is basically two divs -- the top a toolbar, that expands/collapse the second div which can contain other...
5
by: William F. Robertson, Jr. | last post by:
I am having difficulty in diagnosing this problem and was throwing it out for some other eyes. I have derived my own Page class from System.Web.UI.Page and have overridden the LoadViewState...
3
by: Mike | last post by:
Hi, I am adding controls dynamically in a WebForm, but none of these controls' events fire. Here is the class code I am using. I have tried so many things, but nothing works :-( namespace...
0
by: J. | last post by:
Hello, I'm trying to override the LoadViewState-method of a simple page using the .NET Framework 2.0. However, the LoadViewState-method in the code below never gets executed. The...
4
by: Seraph | last post by:
Again, I'm rather new here, so if I fail to follow any etiquette, please forgive me and let me know what I've done wrong, but I think this might interest quite a few people. One of my colleaques...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.