Insert view state data in Page render event (VB) : ViewState « Sessions « ASP.NET Tutorial






<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

File: Default.aspx.vb

Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ViewState("Example") = "java2s"
    End Sub


    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter
        Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)
        MyBase.Render(htmlWriter)
        Dim html As String = stringWriter.ToString
        Dim StartPoint As Integer
        StartPoint = html.IndexOf("<input type=\""hidden\"" name=\""__viewstate\""")
        If (StartPoint >= 0) Then
            Dim EndPoint As Integer = (html.IndexOf("/>", StartPoint) + 2)
            Dim viewstateInput As String
            viewstateInput = html.Substring(StartPoint, (EndPoint - StartPoint))
            html = html.Remove(StartPoint, (EndPoint - StartPoint))
            Dim FormEndStart As Integer = (html.IndexOf("</form>") - 1)
            If (FormEndStart >= 0) Then
                html = html.Insert(FormEndStart, viewstateInput)
            End If
        End If
        writer.Write(html)
    End Sub


End Class








11.5.ViewState
11.5.1.Disable ViewState for a certain control
11.5.2.Trim view state
11.5.3.Use ViewState to store object list (C#)
11.5.4.Use ViewState to store object list (VB)
11.5.5.Insert view state data in Page render event (C#)
11.5.6.Insert view state data in Page render event (VB)