Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 22nd, 2010, 09:16 AM
Registered User
 
Join Date: Apr 2010
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default unable to understand the workings of viewstate - chapter 8

Hi,
I have been trying to understand the workings of viewstate in ch 8.i think i understand that theoretically but then i seemed to not get the workings of it - especially when i attempt the exercise question 2 and modify the code accordingly.
can u please help?

our objective:
1. when the page 1st loads - the AboutUs.aspx will show the banner in left - vertically
thru this code: in Banner.ascx.cs

public Direction DisplayDirection
{
get
{
object displaydirection = ViewState["DisplayDirection"];
if (displaydirection != null)
{
return (Direction)displaydirection;
}
else
{
return Direction.Vertical;
}
}

set
{
ViewState["DisplayDirection"] = value;
}
}

2. if it is a postback:
return (Direction)displaydirection;
would be used.
and besides i think
<Wrox:Banner ID="Banner2" runat="server" DisplayDirection="Horizontal" />
is already set.
so in the subsequent loads the banner is horizontal.

is this what is happening? am i thinking correct??

i would love to get more info on viewstate.
can u please suggest something?

thanks a ton,
sarala
 
Old April 23rd, 2010, 11:41 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

With a normal property (e.g. one not using ViewState) the value would be lost after each post back. Consider this:

if (!Page.IsPostBack)
{
myBanner.NavigateUrl = "SomeUrl";
}

What happens when NavigateUrl is a simple property and the user clicks a button on the page? The following:

1. The page loads
2. Because Page.IsPostBack is not true, the NavigateUrl is set to "SomeUrl"
3. The user clicks a button and posts back
4. Because Page.IsPostBack is now true, the NavigateUrl value is not set. Since it can't keep its own value, the previous value is lost.

When you change the property to a ViewState property, the following happens:

1. The page loads
2. Because Page.IsPostBack is not true, the NavigateUrl is set to "SomeUrl"
3. The user clicks a button and postbacks
4. Because Page.IsPostBack is now true, the NavigateUrl value is not set. However, it stored its value in ViewState in step 2, so it still contains that value.

Does that help?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to run code for Chapter 9 ttdevelop BOOK: Professional Android 2 Application Development 1 April 26th, 2010 06:16 AM
Unable to run Chapter 2 Example tkachuk BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 6 August 2nd, 2009 11:37 PM
Chapter 8 - Implementing ViewState Properties janla2112 ASP.NET 3.5 Basics 4 October 21st, 2008 03:57 PM
Chapter 4: Controls, ViewState and Events BazNZ BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 July 14th, 2008 03:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.