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 29th, 2009, 01:50 PM
Authorized User
 
Join Date: Mar 2009
Posts: 64
Thanks: 0
Thanked 1 Time in 1 Post
Default "Remembering" the value in a DropDownList

Imar,

This is one of those questions, the answer to which is likely in the book. If so, if you would kindly direct me to that spot I will figure it out.

I have a DropDownList that is populated with the current value from a field in the database. I want to "remember" that value. If the user selects another value I then display a button to update the database. If, by chance, the user selects the original value I make the button invisible.

My problem is in remembering the original value. I tried a global variable in the class for the page in the Code Behind but that did not work. It seems that the global variable doesn't persist and loses its memory of the original value upon postback. What I have done as an expedient is to put a label on the page that is invisible and put the value in there and then reference it. That seems ham handed, but it does work.

I thought that this was a state engine issue, but the state engine allows a control to remember its value after postback. I want to remember the original value for comparison.

What am I missing?

Thomas
 
Old April 29th, 2009, 03:47 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Thomas,

Quote:
What am I missing?
Not much. It's certainly a way that works.

To maintain state you have a few other options as well:

1. Session State
Works acros the session but feels like a bit of overkill for this scenario

2. Cookies
Maintains state between browser sessions but feels like a bit of overkill as well for this scenario

3. Hidden form fields
You can add an <asp:Hidden /> control and write the value to it. Works, but feels slightly clumsy.

4. ViewState
The best option, IMO, is ViewState. You can directly write to and read from ViewState like this:
Code:
 
ViewState["SomeThing"] = "SomeValue";
string whatEver = (string) ViewState["SomeThing"];
However, it's much cleaner to wrap this in a ViewState property:
Code:
 
public string SomeValue
{
  get
  {
    object someValue = ViewState["SomeValue"];
    if (someValue != null)
      return (string)someValue;
    else
      return String.Empty;
  }
  set
  {
    ViewState["SomeValue"] = value;
  }
}
This way, you can access the property like any other, but it's value will be persisted in ViewState. Example:

Code:
if (this.SomeValue == "WhatEver")
{
 
}
You can drop the "this" reference; I just added it here to make it clear it's referring to a property of the Page.

Does this help? Let me know if you want a VB example instead.

Cheers,

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
Reading a files general property of "Type of file" GregSivers Visual Basic 2008 Essentials 7 June 3rd, 2009 09:38 AM
Which storage engine for "Post for sell goods" website taev MySQL 2 May 8th, 2009 02:53 PM
Code not going as planned: "icicle" vs "savedinstancestate" joopthecat BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 May 3rd, 2009 03:09 PM
Chapter-5 on Intents:ContactPickerTester does not show the button "Pick a Contact" sunilm12 BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 April 15th, 2009 11:55 AM
How to download Code in "Professional Active Server Page 3.0 " book. renjith0 All Other Wrox Books 2 April 2nd, 2009 05:06 AM





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