LoadViewState not firing when Control added with AddAt

G

Guest

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 ();
}
}
}
 
J

John Saunders

Devin Fensterheim said:
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.
 
S

Scott G.

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
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 ();
}
}
}
 
Joined
Sep 1, 2009
Messages
2
Reaction score
0
Mitigation of the Replay Attacks in a data sharing system using WSQ

I was wondering if one were interested in writing an algorithm for the demodulation of the Replay Attacks using the Wavelet Scalar Quantization merging the Huffman Decoder-Encoder as the designing sequence, what would be the possibility of mitigating the False Acceptance Rate (FAR) and the False Rejection Rate (FRR)?

Thank You.
 
Last edited:
Joined
Sep 1, 2009
Messages
2
Reaction score
0
Is that it or should we expect any more comments on this thread? I don't understand how the related threads at the bottom of the page are related to this thread at all?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top