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