Click here to Skip to main content
15,883,835 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi!
I've got a custom web user control that consists of a panel and 12 imageMaps on it. Now I've added a member variable like this:
C#
ImageMap[] ProfileImages = null;

And in the OnInit method, i do this:
C#
ProfileImages = new ImageMap[12];
 ProfileImages[0] = ImageMap1;
 ProfileImages[1] = ImageMap2;
 ProfileImages[2] = ImageMap3;
...


What I want to do now is to be able to access the image maps using the array in a method like this:
C#
public void DoSomething()
        {
            for (int i = 0; i < 12; i++)
            {
                ProfileImages[i].ImageUrl = "someUrl";
                ProfileImages[i].DescriptionUrl = "anotherUrl";
            }
        }


The problem now is that the array is null because of the page-lifecycle. So I tried to add the variable to the ViewState, like this (also in the OnInit method):
C#
ViewState.Add("ProfileImages", ProfileImages);

And then retrieve it before I use the array, like this (in the DoSomething() method):
C#
ProfileImages = (ImageMap[])ViewState["ProfileImages"];

This has the effect that sometimes the array is not null, but the imageMaps in it are still null.

So how do I correctly conserve my variable across page requests?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900