Corrputed viewstate from a dynamically generated form object

2

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.LoadPageStateFromPersistenceMedium()
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?
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top