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?