473,771 Members | 2,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.P age
{
protected HtmlForm Form;
private void Page_Load(objec t sender, System.EventArg s 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(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
}
public class CC : Control
{
protected override void LoadViewState(o bject savedState) // Never Called
{
ViewState["x"] = new object();
this.Controls.A dd(new LiteralControl( "Load View State"));
base.LoadViewSt ate (savedState);
}
protected override object SaveViewState()
{
ViewState["x"] = new object();
this.Controls.A dd(new LiteralControl( "Save View State Called"));
return base.SaveViewSt ate ();
}
}
}
Nov 18 '05 #1
2 3485
"Devin Fensterheim" <Devin Fe*********@dis cussions.micros oft.com> wrote in
message news:28******** *************** ***********@mic rosoft.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
johnwsaundersii i 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*********@dis cussions.micros oft.com> wrote in message news:28******** *************** ***********@mic rosoft.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.P age
{
protected HtmlForm Form;
private void Page_Load(objec t sender, System.EventArg s 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(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
}
public class CC : Control
{
protected override void LoadViewState(o bject savedState) // Never Called
{
ViewState["x"] = new object();
this.Controls.A dd(new LiteralControl( "Load View State"));
base.LoadViewSt ate (savedState);
}
protected override object SaveViewState()
{
ViewState["x"] = new object();
this.Controls.A dd(new LiteralControl( "Save View State Called"));
return base.SaveViewSt ate ();
}
}
}
Nov 18 '05 #3

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

Similar topics

2
22010
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 on different menu forms. Details: frmTRNMenu is the main menu of the application. It uses tabbed pages to logically separate menu items. The last tab on the list is different - it has an image and then "O&M". Clicking on this last page will...
0
1100
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 object SaveViewState() { object SaveState; SaveState = new object; SaveState = this.chkItem.Checked; return SaveState; }
0
3090
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 postbacks i.e a dropdown list of image folder choices - user selects a folder and then my control will display from that folder. However, in using the load and saveviewstate, the control doesnt work quite right. It takes 2 clicks of the button to...
2
1904
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 works. Can any one help me with this? Thanks, SSW
7
2342
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 modules/controls (eg a newsfeed, quick phonelist, login, etc.) But the controls end up too 'deep': it appears that it would be lighter/better if I could just enherit the uc_login.aspx directly off of the uc_container.aspx so that all modules have the same...
5
5300
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 method. I derive all of my application pages from my own Page. For some reason on two of my pages the LoadViewState derived method is not firing and thus not setting up some of my controls on the page. On the other 240 ish pages, the...
3
2303
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 WebApplication1 { using System;
0
1316
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 SaveViewState-method gets executed on every page request as described in http://msdn.microsoft.com/asp.net/whidbey/default.aspx?pull=/library/en-us/dnvs05/html/internals.asp Can anyone pinpoint the problem? (Or am I just a dumbass?)
4
3990
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 was endeavoring to create a custom user control to make things a bit simpler, but she noticed that her Page_Load eventhandler was firing twice. So after long hours of research and experimentation, I stumbled upon, imho, is quite the discovery. ...
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6712
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.