De-Sludging ASP.NET Pages with PageAdapter, Page 2
Listing 3 contains some simple sample code. Comment out the XML in bold for MyPageAdapter, run the code, and view source. You will see the fairly large VIEWSTATE. Uncomment the code—remove the <!-- --> comment characters—and run the code again. View the source. You will see that the VIEWSTATE is almost all gone. (See Listing 3 for some demo code.)
Listing 3: Some sample code you can use to test you PageAdapter.
Imports System.Collections.Generic
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
If (Not IsPostBack) Then Return
Dim customers As List(Of Customer) = New List(Of Customer)
Dim I As Integer
For I = 1 To 100
customers.Add(New Customer(I, "Customer" + I.ToString()))
Next
GridView1.DataSource = customers
GridView1.DataBind()
End Sub
End Class
Public Class Customer
Private _iD As Integer
Public Property ID() As Integer
Get
Return _iD
End Get
Set(ByVal Value As Integer)
_iD = Value
End Set
End Property
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property
''' <summary>
''' Initializes a new instance of the Customer class.
''' </summary>
''' <param name="iD"></param>
''' <param name="name"></param>
Public Sub New(ByVal iD As Integer, ByVal name As String)
_iD = iD
_name = name
End Sub
End Class
Right-click on your browser and select View Source. Search for VIEWSTATE to see the radical change in the amount of VIEWSTATE information with and without the PageAdapter.
Summary
I am a firm believer in user groups, TechEd, reading, and other such activities. Because I write, it's a little harder for me to find new things for me, but I do find them. Jeff Prosise demonstrated an implementation a PageAdapter and .Browser for ASP.NET 2.0 at TechEd 2007 in Orlando. (Jeff is one smooth presenter; if you get a chance, see him.) I hope you got to go. If not, try to make it next year.
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object oriented programming and .NET. Check out his new book UML DeMystified from McGraw-Hill/Osborne. Paul is an architect for Tri-State Hospital Supply Corporation. You may contact him for technology questions at pkimmel@softconcepts.com.
If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.
Written by Paul Kimmel. pkimmel@softconcepts.com
Copyright © 2007. All Rights Reserved.
