Mega Code Archive

 
Categories / ASP.Net / User Control And Master Page
 

Creating ViewState-Enabled Control Properties (VB)

<%@ Page language="VB" %> <%@ Register TagPrefix="Control" Namespace="Control" Assembly="Control" %> <script language="VB" runat="server"> Sub Page_Load(Sender As Object, E As EventArgs)   If Not IsPostBack Then     Dim RandomGenerator As Random     RandomGenerator = New Random(DateTime.Now.Millisecond)     ViewStateControl1.Text = RandomGenerator.Next(1,100)   End If End Sub </script> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html>   <body ms_positioning="GridLayout">     <form id="Form1" method="post" runat="server">       <Control:viewstatecontrol id="ViewStateControl1" runat="server" />       <asp:linkbutton text="PostBack test" runat="server" id="Linkbutton1" />     </form>   </body> </html> File: Default.vb Imports System Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.ComponentModel Namespace Control     Public Class ViewStateControl         Inherits System.Web.UI.WebControls.WebControl         Public Property Text() As String             Get                 Dim _text As String = CStr(ViewState("Text"))                 If _text Is Nothing Then                     Return String.Empty                 Else                     Return _text                 End If             End Get             Set(ByVal Value As String)                 ViewState("Text") = Value             End Set         End Property         Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)             writer.Write(Text)         End Sub     End Class End Namespace