retrieving values in array & store in session and calling to other page asp.net c#
Hi I want to store values inside the array in session. I want to use it to another page. How i can do that? I used session to store values of array as follow:
int i, randno;
int[] a = new int[5];
for ( i = 0; i < 4; i++)
{
int flag = 0;
Ran开发者_JAVA百科dom rnd = new Random();
randno = rnd.Next(1, 15);
for (int j = 0; j < i; j++)
{
if (a[j] == randno)
flag = 1;
}
if (flag == 0)
{
a[i] = randno;
}
else
{
i--;
}
}
Session["values"] = a;
In other page i used code:
int[] a = (int[])Session["values"];
Response.Write(a);
This code is right? Because its not giving values. But retrieving to another page then it gives last value of the array, on the same page it gives all the values. I want all the values of the array. Asp.net, c# Thank you.
Try this
To store all items of the array.
string[] a = new string[]{"a","b","c"};
Session["values"] = a;
And in the next page, you can retrieve it like this.
string[] a = (string[])Session["values"]
You could store the entire array into session:
Session["values"] = a;
and on the other page:
SomeType[] a = Session["values"] as SomeType[];
// Use the array here
The probblem you are facing is in the last line of code:
Response.Write(a);
It's like ytou are saying
int[].ToString()
and normally, the output will be as you have it right now.
My take will be to store the data as string array and then you can represent it like this:
Response.Write(string.Join(", ", a));
YOu could put it in a class, like so:
Public Class MyGlobals
Private Shared _QuizArray As New ArrayList
Public Property theArray As ArrayList
Get
Return _theArray
End Get
Set(ByVal value As ArrayList)
_theArray = value
End Set
End Property
End Class
Then Access it from various pages like this:
Set Value: QuizArray.Add(index)
Get Value: i = QuizArray(index)
精彩评论