3

I am using Asp.Net With JqueryMobile (C#). I declare string[] roomno = new string[100]; in my Class.

In Page_load Event:-

if (!Page.IsPostBack)
{
    Some Code...
     for (int j = 0; j <= vallen; j++)
     {
         roomno[j] = "A" + Convert.ToString(j + 1);
     }
    Some Code,..
}

When I Click The Button The Array values is clear. So I wanna know How to save Array in View State? And Retrieve That in ASP.NET?

2 Answers 2

4
ViewState["some_key"] = roomno;

roomno = ViewState["some_key"] as string[];

or words to that effect :)

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this but,.. Cannot implicitly convert type 'object' to 'string[]'. An explicit conversion exists (are you missing a cast?) Error is Coming in roomno = ViewState["some_key"];. how to cast?
I know only Convert.Tostring. Thanks.
see the 'as string[]' bit i added to the answer. that is bascially casting to viewstate 'object' to a 'string []', but if the object isn't castable to a 'string []' then you will et a null value rather than an InvalidCastException. So, you may have to do a null check in your code.
0
ViewState["SomeKey"] = (string[])roomno;
roomno = (string[])ViewState["SomeKey"];

Or to initialize

string[] roomno = string[0]; 
ViewState.Add("SomeKey", roomno);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.