Hi Steven,
thanx for the reply. I did leave out one part that the
page I was basing the main page on was a modified one too,
that got me past the corrupt viewstate error it is as
follows:
public class NVSPage:System.Web.UI.Page
{
public NVSPage():base()
{
}
protected override object
LoadPageStateFromPersistenceMedium()
{
return null;
}
protected override void
SavePageStateToPersistenceMedium(object viewState)
{
}
}
this is ok, because on the main page I don't care about
the viewstate....
now here comes the funny part, after many hours looking at
the base page class with Anakrino I finally relized
something, that the page was working correctly, but ran
across something wierd. That the textbox class isn't
holding state in my page class. If i replace my textbox
controls with calendars it works great. ie....
private void myPage_Load(object sender, EventArgs e)
{
//System.Web.UI.WebControls.TextBox t1 = new
System.Web.UI.WebControls.TextBox();
MyTextBox t1 = new MyTextBox();
t1.ID = "myTextBox";
t1.EnableViewState=true;
Calendar c1 = new Calendar();
c1.ID = "myCalender1";
Button b1 = new Button();
b1.ID="myButton1";
((System.Web.UI.HtmlControls.HtmlForm)
((System.Web.UI.Page)sender).FindControl
("myForm")).Controls.Add(t1);
((System.Web.UI.HtmlControls.HtmlForm)
((System.Web.UI.Page)sender).FindControl
("myForm")).Controls.Add(c1);
((System.Web.UI.HtmlControls.HtmlForm)
((System.Web.UI.Page)sender).FindControl
("myForm")).Controls.Add(b1);
}
but you may have noticed something else that I found out.
In this senerio everything works great with everything
else, but to get the textbox to work I had to create my
own textbox class, inherited from it. You may be
wondering why this works. I have no idea, because all I
did was create an inherited control, but added nothing to
it:
public class MyTextBox:System.Web.UI.WebControls.TextBox
{
public MyTextBox():base()
{
}
}
Thats it. This has to be a bug or something. It makes no
sense. Of course textboxes work great in pages create the
normal asp.net way, but for some reason, to create pages
the way I did it would not hold viewstate until i did
this. Why is this? Is there something else I'm not doing.
So In the end, I can do what I want which is to create
multiple page objects adding controls to them and
processing them, holding state correctly etc, except on
this case with the TextBox control. Wierd huh? Anyway
I'll go ahead and post almost all the code for you to look
at:
public class MyPage:System.Web.UI.Page
{
StringBuilder _myStringBuilder;
public MyPage():base()
{
_myStringBuilder = new
StringBuilder();
}
public StringBuilder myStringBuilder
{
get{return _myStringBuilder;}
}
protected override object
LoadPageStateFromPersistenceMedium()
{
Triplet thisisit = (Triplet)Session
["ViewState" + this.ID];
return Session["ViewState" +
this.ID];
}
protected override void
SavePageStateToPersistenceMedium(object viewState)
{
Triplet thisisit = (Triplet)
viewState;
Session["ViewState" + this.ID] =
viewState;
}
protected override void Render
(System.Web.UI.HtmlTextWriter writer)
{
_myStringBuilder = new
StringBuilder();
System.IO.StringWriter sw = new
System.IO.StringWriter(_myStringBuilder);
System.Web.UI.HtmlTextWriter htw =
new System.Web.UI.HtmlTextWriter(sw);
base.Render (htw);
_myStringBuilder.Replace
("__doPostBack","__doPostBack" + this.ID);
}
protected override void OnInit(EventArgs e)
{
base.OnInit (e);
this.RegisterViewStateHandler();
}
protected override void AddParsedSubObject
(object obj)
{
base.AddParsedSubObject (obj);
}
}
public class NVSPage:System.Web.UI.Page
{
public NVSPage():base()
{
}
protected override object
LoadPageStateFromPersistenceMedium()
{
return null;
}
protected override void
SavePageStateToPersistenceMedium(object viewState)
{
}
}
public class WebForm1 : Anothersomething.NVSPage
{
protected
System.Web.UI.WebControls.Literal Literal1;
private void Page_Load(object sender,
System.EventArgs e)
{
// Put user code to initialize the
page here
MyPage myPage = new MyPage();
myPage.ID = "word";
myPage.EnableViewState=true;
myPage.Load+=new EventHandler
(myPage_Load);
System.Web.UI.HtmlControls.HtmlForm myForm = new
System.Web.UI.HtmlControls.HtmlForm();
myForm.ID = "myForm";
myPage.Controls.Add(myForm);
((IHttpHandler)
myPage).ProcessRequest(Context);
Literal1.Text =
myPage.myStringBuilder.ToString();
myPage = new MyPage();
myPage.ID = "word2";
myPage.EnableViewState=true;
myPage.Load+=new EventHandler
(myPage2_Load);
myForm = new
System.Web.UI.HtmlControls.HtmlForm();
myForm.ID = "myForm2";
myPage.Controls.Add(myForm);
((IHttpHandler)
myPage).ProcessRequest(Context);
Literal1.Text +=
myPage.myStringBuilder.ToString();
}
#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
private void myPage_Load(object sender,
EventArgs e)
{
//System.Web.UI.WebControls.TextBox t1 = new
System.Web.UI.WebControls.TextBox();
MyTextBox t1 = new MyTextBox();
t1.ID = "myTextBox";
t1.EnableViewState=true;
Calendar c1 = new Calendar();
c1.ID = "myCalender1";
Button b1 = new Button();
b1.ID="myButton1";
((System.Web.UI.HtmlControls.HtmlForm)
((System.Web.UI.Page)sender).FindControl
("myForm")).Controls.Add(t1);
((System.Web.UI.HtmlControls.HtmlForm)
((System.Web.UI.Page)sender).FindControl
("myForm")).Controls.Add(c1);
((System.Web.UI.HtmlControls.HtmlForm)
((System.Web.UI.Page)sender).FindControl
("myForm")).Controls.Add(b1);
}
private void myPage2_Load(object sender,
EventArgs e)
{
//System.Web.UI.WebControls.TextBox t1 = new
System.Web.UI.WebControls.TextBox();
MyTextBox t1 = new MyTextBox();
t1.ID = "myTextBox2";
t1.EnableViewState=true;
Calendar c1 = new Calendar();
c1.ID = "myCalender2";
Button b1 = new Button();
b1.ID="myButton2";
((System.Web.UI.HtmlControls.HtmlForm)
((System.Web.UI.Page)sender).FindControl
("myForm2")).Controls.Add(t1);
((System.Web.UI.HtmlControls.HtmlForm)
((System.Web.UI.Page)sender).FindControl
("myForm2")).Controls.Add(c1);
((System.Web.UI.HtmlControls.HtmlForm)
((System.Web.UI.Page)sender).FindControl
("myForm2")).Controls.Add(b1);
}
}
public class MyTextBox:System.Web.UI.WebControls.TextBox
{
public MyTextBox():base()
{
}
}
-----Original Message-----
Hi Kevin,
Thank you for using Microsoft Newsgroup Service. Based on
your description,you created a custom page class which override the Method
when Loading andsaving the page's viewstate. Also, you use this class to
render anotherform tag in a asp.net page which have a original form
tag. However, whenyou run the page and fire the textbox's change event ,
there occured the"The viewstate is invalid for this page and might be
corrupted" exception.And you thought it caused by the ViewState which is not
correctly loadedfrom "session" where you manually stored the viewstate of
the custom pagein. Please correct me if my understanding of your problem
is not quiteexact.
I've read the code you provided and also had some tests
on my side. Yes, Ialso encountered the problem you mentioned. However, I
don't think it isbecause the viewstated not correctly retrieved
from "session". I've addsome break points in
the "LoadPageStateFromPersistenceMedium()" method, infact, when the exception occured, the runtime even hasn't
entered thisfunction. That is said, the exception occured before
retrieving data fromviewstate. I think the exception is likely caused by the
ASP.NET runtime'svalidation for the viewstate:
When an ASP.NET page receives a post, it checks for a
field called__VIEWSTATE (that's 2 underscore symbols) in the post.
ASP.NET is usingthis field for many reasons, most outside the scope of
this article. But,one thing the __VIEWSTATE field does contain is internal
validation forASP.NET. If you simply post the __VIEWSTATE field to a
different ASP.NETpage, than the page that filled the __VIEWSTATE field,
ASP.NET will throwan exception:
"The viewstate is invalid for this page and might be
corrupted."
Since in the test page, we after rendering and view its
html source in IE,you could find that there will be two form tags in it and
both has thepostback client script functions but only the original
page's form tag hasthe _VIEWSTATE hidden area. Thus, when the page is post
back via thosepostback client functions(not submit). That'll cause the
ASP.NET runtime tocheck the viewstate and found that the _VIEWSTATE doesn't
match the"page"(in fact, the two form tags will be processed using
two differentpage class) , so the "The viewstate is invalid for this
page and might becorrupted." came out.
I think you may try remove the "_VIEWSTATE" hidden area (
just change itsname) using client side script before the post back event
and also addbreak points to trace the custom page class's viewstate
opeations to seewhether the problem remain.
If you have any questions on it or if you have any new
findings, please letme know.
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties,
and confers norights.)
.