Im trying to make a get and set to make my List<> persistent. I think my intention is clear with the code. I want the List to remain throughout postbacks so i dont loose data in it.
What am i doing wrong? Thanks to my AlertPopUp i can see that the Get is triggered, but never the Set. So the List comes back as containint zero items when it should have several items.
private List<string> accountIDsSelectedForDeletion = new List<string>();
public List<string> AccountIDsSelectedForDeletion
{
get {
if (ViewState["AccountIDsSelectedForDeletion"]!= null)
{
accountIDsSelectedForDeletion = ViewState["AccountIDsSelectedForDeletion"] as List<string>;
AlertPopUp.QuickDebugMessage("getting list from viewstate. Count: "+ accountIDsSelectedForDeletion.Count);
}
AlertPopUp.QuickDebugMessage("returning list");
return accountIDsSelectedForDeletion;
}
set {
AlertPopUp.QuickDebugMessage("setting list to viewstate. Count: " + accountIDsSelectedForDeletion.Count);
accountIDsSelectedForDeletion = value;
ViewState["AccountIDsSelectedForDeletion"] = accountIDsSelectedForDeletion;
}
In a nutshell, yes.
.Add()is mutating the list, while the Set property acts on the list as a set piece.To accomplish what your trying to do here, I would create an inherited type from List, so that I could inject code into the
AddRemove(ext, there are more) methods. Like:Or have a class that wraps a private List<> and manages the Add/Remove methods, maybe like:
Then just replace List with this type on your page level class.
By the way, persisting to the
ViewStatebloats the amount of information you have to send up and down the wire per page load. Have you consideredSession, or theCacheobject?