Skip to Content
Beginning ASP.NET 3.5: In C# and VB
book

Beginning ASP.NET 3.5: In C# and VB

by Imar Spaanjaars
March 2008
Intermediate to advanced content levelIntermediate to advanced
766 pages
21h 15m
English
Wrox
Content preview from Beginning ASP.NET 3.5: In C# and VB

A.8. Chapter 8

A.8.1.

A.8.1.1.
A.8.1.1.1. Exercise 1 solution

A standard property uses a normal backing variable to store its value, whereas a ViewState property uses the ViewState collection for this. A normal property is reset on each postback, whereas a ViewState property is able to maintain its value. This advantage of the ViewState property comes at a cost, however. Storing the value in ViewState adds to the size of the page, both during the request and the response. A normal property doesn't have this disadvantage. You should carefully consider what you store in ViewState to minimize the page size.

A.8.1.1.2. Exercise 2 solution

To make the property maintain its state across postbacks, you need to turn it into a ViewState property. The required code is almost identical to that of the NavigateUrl, but it uses the Direction data type instead of a string. For the VB.NET example, just modify the highlighted Get and Set parts of the property. For the C# example, you need to remove the automatic property and replace it with the following code.

VB.NET

Public Property DisplayDirection() As Direction
  Get
    Dim _displayDirection As Object = ViewState("DisplayDirection")
    If _displayDirection IsNot Nothing Then
      Return CType(_displayDirection, Direction)
    Else
      Return Direction.Vertical ' Not found in ViewState; return a default value
    End If
 End Get
 Set(ByVal Value As Direction)
    ViewState("DisplayDirection") = Value
 End Set
End Property

C#

public Direction DisplayDirection { get { ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning ASP.NET 4: in C# and VB

Beginning ASP.NET 4: in C# and VB

Imar Spaanjaars
Professional ASP.NET 3.5 SP1 Edition: In C# and VB

Professional ASP.NET 3.5 SP1 Edition: In C# and VB

Bill Evjen, Scott Hanselman, Devin Rader

Publisher Resources

ISBN: 9780470187593Purchase book