Store List in ViewState

0

I want to build a list of strings and save them to a viewstate, and use them later. Ex

List<string> listaNomes = new List<string>();
foreach(var algo in TesteList)
{
listaNomes.add(algo.id);
//salve essa lista em ViewState
}

I'm saving several strings in this list, but need to manipulate it elsewhere on the page, like saving in ViewState and manipulating later?

    
asked by anonymous 12.03.2015 / 19:03

1 answer

0

If you want to use this information on another page you must use the session and not the viewState, ViewState keeps the information only on the page you are on.

Use Session as follows.

Assigning the List to Session.

List<string> listaNomes = new List<string>();

    listaNomes.Add("Marconi");
    listaNomes.Add("Marcos");

    Session["Lista"] = listaNomes;

Retrieving the Session list on another page or on the same page.

List<string> lista = (List<string>)Session["Lista"];

If you do not need to use it anymore you can remove the session item.

Session.Remove("Lista");
    
12.03.2015 / 19:12