Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 19th, 2006, 05:03 AM
Authorized User
 
Join Date: Jun 2003
Location: St. Louis, Mo., USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rfinks
Default Generating ViewState Names Dynamically

I'm trying to store values in ViewState via a name I'm constructing dynamically. Following is the code I'm using to store values in ViewState:

    strTmpVar = "PageKey" & CStr(intCounter)
    ViewState(""" & strTmpVar & """) = colArrayList(tmpPage).PageNumber

I'm convinced that the way I'm constructing the name of the ViewState item is not working the way I had thought it would. When I retrieve the ViewState items I get the value 25 for all items.

Code used to retrieve values from ViewState:

    strTmpVar = "PageKey" & CStr(intCounter)
    intPageNum = CInt(ViewState(""" & strTmpVar & """))

Also I'm storing the name/value pairs in ViewState during the Page_Load when not a PostBack and retrieving the name/value pairs in Page_Load during a PostBack.

Is it possible to do what I'm trying to do and if so does anyone know the correct way to do it.

Thanks
Roger Finks
Reply With Quote
  #2 (permalink)  
Old July 19th, 2006, 05:31 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

.NET has built in a key/value method for the ViewState functionality. You [u]must</u> use this methodolgy, or you are going to be writing buggy code till midnight!
Values can be extracted from viewstate in exactly the same way, too.
I forget the syntax, but it is like: ViewState.Add("PageKey", strTmpVar)
Reply With Quote
  #3 (permalink)  
Old July 25th, 2006, 09:27 PM
Authorized User
 
Join Date: Jun 2003
Location: St. Louis, Mo., USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rfinks
Default

I finally resolved the problem I was having. I did not fully explain why I was trying to generate dynamic name / value pairs. I am loading an object into a arrayList and needed to store the ArrayList in ViewState. I was convinced that I needed to unload all of the object's fields from the arrayList.

The answer was so easy I almost couldn't believe it.
1. Construct the Class as Serializable

    <Serializable()> Public Class myClass
    .
    .
    End Class

2. Store the arrayList like any other variable

    ViewState("myArrayList") = myArrayList

3. Retrieve the ArrayList from ViewState:

    myArrayList = New ArrayList
    myArrayList = CType(ViewState("myArrayList"), ArrayList)

Bingo, it was that simple.
Reply With Quote
  #4 (permalink)  
Old July 31st, 2006, 11:47 AM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Yeah, .NET has really made a lot of things unbelievably easy!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically generating Javascript (XSLT in IE6) einszwei XSLT 1 June 28th, 2007 01:07 PM
dynamically generating Excel file sunitapandey Struts 3 November 23rd, 2006 03:53 AM
Dynamically Generating Textboxes Maxood PHP How-To 6 March 9th, 2006 09:14 AM
Generating html tags dynamically sachin lad Servlets 1 April 26th, 2005 05:35 PM



All times are GMT -4. The time now is 08:24 AM.


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