Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6925567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:48:21+00:00 2026-05-27T10:48:21+00:00

Im trying to make a get and set to make my List<> persistent. I

  • 0

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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:48:22+00:00Added an answer on May 27, 2026 at 10:48 am

    Could it be because i change the List by its Add() method?

    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 Add Remove (ext, there are more) methods. Like:

    //not syntatically correct sorry
    public class MyList : List<string>
    {
        public override Add(string NewItem)
        {
            //do a extra peristance step here
            //
            me.Add(NewItem);
        }
    }
    

    Or have a class that wraps a private List<> and manages the Add/Remove methods, maybe like:

    public class MyList
    {
        private List<string> listItems { get; set; }
        //don't expose the list in a mutable state, IEnumerable instead
        public IEnumerable<string> Items { get { return this.listItems; } }
    
        public void Add(string NewItem)
        {
            this.ListItems.Add(NewItem);
        }
    }
    

    Then just replace List with this type on your page level class.

    By the way, persisting to the ViewState bloats the amount of information you have to send up and down the wire per page load. Have you considered Session, or the Cache object?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't seem to figure this out, see code below. Trying to make a GET
I am trying to get a list together of tools/libraries that make me more
I'm trying to make a dynamic array in C# but I get an annoying
I'm trying to make a drop down menu using javascript/jquery but can't get my
I'm trying to make some text bold using HTML, but I'm struggling to get
I'm trying to make my C# application multi threaded because sometimes, I get an
Hi iam trying to make an update trigger in my database. But i get
I'm trying to make a notification in Android. But i get the error in
I've been trying to get input with IUP to make a small pong game.
I have been trying to get yui-css grid system to make a three column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.