473,396 Members | 1,982 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,396 software developers and data experts.

Using a Page object within a Page object

Ok, before anyone freaks out, I have a solution I need to
create that gathers content from maybe different places.
Each one can return a <form> in the html, so its the
classic can't have more than one runat=server form on a
asp.net page. However, I still want developers to be able
to use asp.net controls to create some apps that are
created on the page. So I need multiple forms on a
asp.net page(doesn't everyone). I purchased the Wilson
Webform deal that got me started, but not quite all the
way there. My basic problems is when I tell the seperate
page to render it does, but the viewstate calls return
nothing....here comes some code:

the Page Class I created as follows, some comments inline

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()
{
return Session["ViewState" +
this.ID];
}
protected override void
SavePageStateToPersistenceMedium(object viewState)
{
//here is where my problem begins, when called
//viewstate == null
Session["ViewState" + this.ID] =
viewState;
}

protected override void Render
(System.Web.UI.HtmlTextWriter writer)
{
//I "steal" the html because if i don't it
//shows up on my main page
_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);
}

protected override void OnInit(EventArgs e)
{
//wilson form says I have to do this not sure
base.OnInit (e);
this.RegisterViewStateHandler();
}


-=-=-=-=-
Ok that being the Page class I create my page and execute
it with the following:

in the code behind page.....

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();
}
private void myPage_Load(object sender,
EventArgs e)
{
TextBox t1 = new TextBox();
t1.AutoPostBack=true;
t1.ID = "myTextBox";

((System.Web.UI.HtmlControls.HtmlForm)
((System.Web.UI.Page)sender).FindControl
("myForm")).Controls.Add(t1);

}

}

This all works great, textbox gets the postback info and
everything, the only thing is that viewstate object
in "protected override void
SavePageStateToPersistenceMedium(object viewState)" comes
back as null.

Anyone have any idea, I assume I'm not calling the Page
object correctly, or somehow need to instantiate the
viewstate or something. I've been fighting this for a
week now. Any suggestions would be greatly appreciated....

Thanx in advance...
Nov 18 '05 #1
4 2784
Hi Kevin,

Thank you for posting in MS Newsgroup. I am trying to find proper resource
to assist you on this issue and will update you as soon as posible.

Regards,

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #2
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 and
saving the page's viewstate. Also, you use this class to render another
form tag in a asp.net page which have a original form tag. However, when
you 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 loaded
from "session" where you manually stored the viewstate of the custom page
in. Please correct me if my understanding of your problem is not quite
exact.

I've read the code you provided and also had some tests on my side. Yes, I
also encountered the problem you mentioned. However, I don't think it is
because the viewstated not correctly retrieved from "session". I've add
some break points in the "LoadPageStateFromPersistenceMedium()" method, in
fact, when the exception occured, the runtime even hasn't entered this
function. That is said, the exception occured before retrieving data from
viewstate. I think the exception is likely caused by the ASP.NET runtime's
validation 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 using
this field for many reasons, most outside the scope of this article. But,
one thing the __VIEWSTATE field does contain is internal validation for
ASP.NET. If you simply post the __VIEWSTATE field to a different ASP.NET
page, than the page that filled the __VIEWSTATE field, ASP.NET will throw
an 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 the
postback client script functions but only the original page's form tag has
the _VIEWSTATE hidden area. Thus, when the page is post back via those
postback client functions(not submit). That'll cause the ASP.NET runtime to
check the viewstate and found that the _VIEWSTATE doesn't match the
"page"(in fact, the two form tags will be processed using two different
page class) , so the "The viewstate is invalid for this page and might be
corrupted." came out.

I think you may try remove the "_VIEWSTATE" hidden area ( just change its
name) using client side script before the post back event and also add
break points to trace the custom page class's viewstate opeations to see
whether the problem remain.

If you have any questions on it or if you have any new findings, please let
me know.
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #3
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.)

.

Nov 18 '05 #4
Hi Kevin,
Thank you for the prompt response. I'm glad that you've found the main
problem in our issue. Yes, I did found that something strange with the
TextBox since I've add a Button correctly in the former test. And as you
said that you got it work correctly when using a custom TextBox just
inherit from the dotnet buildin class, I'm also a bit puzzled with this
situation. Maybe this is really an issue of the ASP.NET since we seldom use
a page object like this, hehe. Any way, I'll keep this issue in mind.
Thanks again for the information you provided.
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 18 '05 #5

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

Similar topics

4
by: Oscar | last post by:
I am quite new to the ASP scene. I want to display the results of a recordset into a third party grid control called VSFlexgrid. I've prepared one asp page which queries an Access database and...
9
by: Charley Kyd | last post by:
I'm a newbie who needs advice about how to use external files of JavaScript code. I spent an hour this afternoon browsing through JavaScript books at the local book store. In about 15 different...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
0
by: Nigel | last post by:
I successfully create a .NET Component (Visual Basic .NET) that would print, unfortunately when used within a web browser it appears that .NET security doesn't allow you to run code that interacts...
3
by: nevets2001uk | last post by:
Hi. I've just started my second ASP.NET (VB) app and I'm using codebehind this time. I'm not using visual studio but am instead coding it all in notepad (HTML, ASP.NET and CSS) I'm trying to...
13
by: Bijoy Naick | last post by:
My project contains multiple aspx pages. Many of these pages have code-behind that use several helper functions. Instead of copying each helper function into each aspx page, I am thinking of...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
7
by: Jim in Arizona | last post by:
I'm brand new at ajax. In fact, about 20 minutes ago was the first time I got it to work. The problem I'm having on another page did not work, however. I'm running into the following error: ...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
0
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,...

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.