Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
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 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 July 19th, 2006, 05:03 AM
Authorized User
 
Join Date: Jun 2003
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
 
Old July 19th, 2006, 05:31 PM
Friend of Wrox
 
Join Date: Nov 2004
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)
 
Old July 25th, 2006, 09:27 PM
Authorized User
 
Join Date: Jun 2003
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.
 
Old July 31st, 2006, 11:47 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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





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





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