473,405 Members | 2,262 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,405 software developers and data experts.

Corrputed viewstate from a dynamically generated form object

234
I'm playing around with page inheritence and I'm to the point now where I
want to create a form object on a derived page. Here's a truncated version
of my 'base' page from which all/most pages in the application would be
derived:
-------------------------
Public Class clsBasePage
Inherits System.Web.UI.Page

Private objForm as new HtmlForm
Public Event ReadyforPageContent()

Public ReadOnly Property HTMLForm() As HTMLForm
Get
Return objForm
End Get
End Property

Protected Overloads Overrides Sub Render(byval writer as [etc.])

With objForm
.Enctype = "application/x-www-urlencoded"
.EnableViewState = True
.ID = "frmForm"
End With

Me.Controls.Add(objForm)

RaiseEvent ReadyForPageContent()

End Sub

End Class
-------------------------

On any page that derives from it, base page should be providing a form
object. Next, I derive a page from clsBasePage, and this particular uses
the form object to create your typical login UI - two textboxes and a
button:

-------------------------
Public Class clspgeLogin
Inherits clsBasePage

Private txtUsername As New Textbox
Private txtPassword As New Textbox
Private WithEvents btnLogin as new Button

Private Sub RenderPageContent() Handles MyBase.ReadyForPageContent

With Me.HTMLForm
.Controls.Add(txtUsername)
.Controls.Add(txtPassword)
.Controls.Add(btnLogin)
.RenderControl(Me.HTMLTextWriter) 'writer from basePage
End With

End Sub
-------------------------

At run time I can visit clspgeLogin and I see the username textbox, the
password textbox and the login button. When I hit the login button and
attempt a postback I get this message:

The viewstate is invalid for this page and might be corrupted

Stack:

System.Web.UI.Page.LoadPageStateFromPersistenceMed ium()
System.Web.UI.Page.LoadPageviewState()
System.Web.UI.Page.ProcessRequestMain()

Viewing the source before I attempt the postback, the hidden "__VIEWSTATE"
form element is there, it's value is nullstring. Most of what I search
around for regarding this error is related to performing server transfers &
web farms, but I'm just posting back 1 page to the same server that made it.
Am I going about this all wrong, missing something subtle, or both?
Nov 19 '05 #1
1 1271
234
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

Due to logistics in a pages lifecycle, the form object should be inserted
into the base pages control collection in the Init event, not the Render
event, if it is to maintain viewstate. This resolved the corrupted
viewstate issue, and postback now operates as expected.

"234" <23*@sdf.com> wrote in message
news:GJ********************@speakeasy.net...
I'm playing around with page inheritence and I'm to the point now where I
want to create a form object on a derived page. Here's a truncated
version of my 'base' page from which all/most pages in the application
would be derived:
-------------------------
Public Class clsBasePage
Inherits System.Web.UI.Page

Private objForm as new HtmlForm
Public Event ReadyforPageContent()

Public ReadOnly Property HTMLForm() As HTMLForm
Get
Return objForm
End Get
End Property

Protected Overloads Overrides Sub Render(byval writer as [etc.])

With objForm
.Enctype = "application/x-www-urlencoded"
.EnableViewState = True
.ID = "frmForm"
End With

Me.Controls.Add(objForm)

RaiseEvent ReadyForPageContent()

End Sub

End Class
-------------------------

On any page that derives from it, base page should be providing a form
object. Next, I derive a page from clsBasePage, and this particular uses
the form object to create your typical login UI - two textboxes and a
button:

-------------------------
Public Class clspgeLogin
Inherits clsBasePage

Private txtUsername As New Textbox
Private txtPassword As New Textbox
Private WithEvents btnLogin as new Button

Private Sub RenderPageContent() Handles MyBase.ReadyForPageContent

With Me.HTMLForm
.Controls.Add(txtUsername)
.Controls.Add(txtPassword)
.Controls.Add(btnLogin)
.RenderControl(Me.HTMLTextWriter) 'writer from basePage
End With

End Sub
-------------------------

At run time I can visit clspgeLogin and I see the username textbox, the
password textbox and the login button. When I hit the login button and
attempt a postback I get this message:

The viewstate is invalid for this page and might be corrupted

Stack:

System.Web.UI.Page.LoadPageStateFromPersistenceMed ium()
System.Web.UI.Page.LoadPageviewState()
System.Web.UI.Page.ProcessRequestMain()

Viewing the source before I attempt the postback, the hidden "__VIEWSTATE"
form element is there, it's value is nullstring. Most of what I search
around for regarding this error is related to performing server transfers
& web farms, but I'm just posting back 1 page to the same server that made
it. Am I going about this all wrong, missing something subtle, or both?

Nov 19 '05 #2

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

Similar topics

2
by: theo | last post by:
Hi... I wish to extract the text content of an Xml file and assign it to DropDownList controls at runtime.I can get the Xml file text content into the DropDownList controls (Ex...if 5 Xml text...
2
by: Abel | last post by:
Hello - How can I utilize ViewState when my webform has dynamically generated controls? Is there a way to add/retrieve values to/from viewstate so that I can use ViewState? I'd rather not...
9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
6
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone...
3
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event...
1
by: Ralph Soons | last post by:
Hi all, I am trying to save the viewstate in a session instead of storing it in a hidden of the webpage which is default. This because of performance reasons. When I use line 2 in combination...
2
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid...
10
by: Robert | last post by:
I have an app that was originally 1.1, now migrated to 2.0 and have run into some sporadic viewstate errors...usually saying the viewstate is invalid, eventvalidation failed or mac error. My web...
2
by: Jarod | last post by:
Hey I change backColor of linkButton in my Page_Load: lbDoSth.BackColor = Color.Red; But I have multiView on this page. On one of the views I have detailsView. When I add a new row a set...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.