<div>can somebody help me?</div> <div>I want to store a List<> in ViewState, but don't works</div> <div>I try this: </div> <div>
//My class Inventory is [Serializable]
public List<Inventory> listInventory = new List<Inventory>();
listInventory = Consult.ConsultInventory(company); //method return list
ViewState["lInvCnt"] = listInventory ;//<--I want to store my list, don't works
//I try this too:
ViewState["lInvCnt"] = listInventory.ToArray();//don't works
Make sure Inventory is marked Serializable like so in the class definition Without being marked with the Serializable attribute data can't be serialized and de-serialized to/from the ViewState.
[Serializable]
public class Inventory
If you want your list to be consistant with what's in the viewstate, ie: updating it saves the updated viewstate values, you could try the folloing
public List<Inventory> listInventory
{
get
{
if(ViewState["listInventory"] != null)
return (List<Inventory>)ViewState["listInventory"];
else
{
// fill a temporary variable just when the ViewState doesn't have
// any data yet.
List<Inventory tempValue = Consult.ConsultInventory(company);
if(tempValue != null)
{
// if the object isn't null, save it in the viewstate and return it
ViewState["listInventory"] = tempValue;
return tempValue;
}
else
{
// it's null so we have an opportunity here to either
// create an empty list and return it:
tempValue = new List<Inventory>();
ViewState["listInventory"] = tempValue;
return tempValue;
// or, return null if we don't want a new list created automatically
return null;
}
}
}
set
{
// the setter of the property always updates the ViewState
ViewState["listInventory"] = value;
}
}
This let's you not have to worry about messing up a call to the ViewState (ie, misspelling the variable name in one spot will throw everything off) and it will also give you intellisense and ensure everything is strongly typed.
Don't forget to mark useful responses as Answer if they helped you towards a solution.
Thanks Markfitzme, right now I'm not working in my class, because I was losing a lot of time trying to do this, so for now I have postponed, but I will put into practice what you propose.
None
0 Points
2 Posts
Store List <> in ViewState
Apr 17, 2014 02:32 PM|Rojas90|LINK
c viewstate asp.net list csharp
All-Star
101931 Points
20703 Posts
Re: Store List <> in ViewState
Apr 17, 2014 02:36 PM|MetalAsp.Net|LINK
It should work. Put a breakpoint and make sure the last is not empty. Also, are you getting an error?
All-Star
26071 Points
5892 Posts
Re: Store List <> in ViewState
Apr 17, 2014 02:59 PM|markfitzme|LINK
Make sure Inventory is marked Serializable like so in the class definition Without being marked with the Serializable attribute data can't be serialized and de-serialized to/from the ViewState.
If you want your list to be consistant with what's in the viewstate, ie: updating it saves the updated viewstate values, you could try the folloing
This let's you not have to worry about messing up a call to the ViewState (ie, misspelling the variable name in one spot will throw everything off) and it will also give you intellisense and ensure everything is strongly typed.
None
0 Points
2 Posts
Re: Store List <> in ViewState
May 01, 2014 04:19 PM|Rojas90|LINK
Thanks Markfitzme, right now I'm not working in my class, because I was losing a lot of time trying to do this, so for now I have postponed, but I will put into practice what you propose.
I'll tell you if I resolved it. Regards
P.S.
I'm sorry if my English is not very good
c asp.net