0

I am pretty sure this question would have been asked before but I cannot find it so please post the link to where I can go to it if so..

However, I persist some data on a Postback in asp.net using a ViewState. The ViewState contains a collection of my own custom object. e.g.

 ViewState["List"] as List<Animal>

Is it possible I can get this list and pass it to the client-side? so really, can it be serialised?

I have tried:

// HTML
<asp:TextBox ID="theList" runat="server" style="display:none"></asp:TextBox> 

 //Sever Side
theList.Text = ViewState["List"].Tostring();

//JS
document.getElementById('theList').innerText;

but of course, this isn't my data this contains the namespace.

My first thoughts was to use JSON.Parse on the client, however I need to pass the data first which is the main issue.

Thanks

user3428422
  • 4,300
  • 12
  • 55
  • 119
  • You look something like http://stackoverflow.com/questions/13437820/how-to-store-list-of-object-into-viewstate/13437873#13437873 – Aristos Jan 29 '15 at 14:35
  • Client-side you will probably need to use hidden fields fro clientside access of viewstate var, see this: http://stackoverflow.com/questions/6980714/how-to-access-viewstate-using-javascript – David Tansey Jan 29 '15 at 14:37
  • You have a List and you want to store it in a TextBox client-side??? I would think you would want to store it client-side as an array in javscript. I could give you some code to do that. If you just want a serialized string, then it might be best to place it in a hidden field. – David P Jan 29 '15 at 14:43
  • Hi @DavidP, nah of course I never wanted it into a textbox, I was demonstrating how it is usually done with a genetic variable so readers understood what I wanted. that would be insane. preferably, I would like to parse the data from a var variable like you would do in an AJAX call.. can this be done? – user3428422 Jan 29 '15 at 15:04

2 Answers2

1

user3428422 has it right (he/she beat me to the post). You might also consider:

    public List<Animal> AnimalList
    {
        get
        {
            if (!(ViewState["lAnimalList"] is List<Animal>))
            {
                ViewState["lAnimalList"] = new List<Animal>();
            }
            return (List<Animal>)ViewState["lAnimalList"];
        }
    }

and then in your Page_Load event have:

        JavaScriptSerializer jss = new JavaScriptSerializer();
        HiddenField.Value = jss.Serialize(AnimalList);

Its the same idea though. You should reward user3428422 first and foremost.

David P
  • 2,027
  • 3
  • 15
  • 27
  • I am the poster and the answerer of the question lol, but I will award the answer to you, so I am not greedy :) – user3428422 Feb 03 '15 at 13:32
0

Ok to do this I just used a JavaScriptSerializer in the server-side

 var serializer = new JavaScriptSerializer();
 var serializedResult = serializer.Serialize(ViewState["List"]);

Then transfer this to a hidden field

hiddenField.Value = serializedResult; 

And now all the data will be in JSON format on the cilent and then you can de-serialise it if you shall wish

user3428422
  • 4,300
  • 12
  • 55
  • 119