Mark Hollas
Mark Hollas

Reputation: 1137

C# List items empty after button clicked

I am trying to understand the lifecycle that is taking place here. I have a asp list view where I get item ids and write them to a list like so.

 protected void ShareWith_OnItemBound(object sender, ListViewItemEventArgs e)
    {
        if (!IsPostBack)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                ListViewDataItem currentItemId = (ListViewDataItem)e.Item;
                System.Web.UI.WebControls.DataKey currentDataKey = this.lvShareWithPanel.DataKeys[currentItemId.DataItemIndex];
                int FriendId = Convert.ToInt32(currentDataKey["SharedUserId"]);
                CurrentList.Add(FriendId);
            }
        }
    }

Where my list is defined outside the method

private List<int> CurrentList = new List<int>();

after, the user adds some new items to the list then click on an asp button to continue. I am running a comparison of the current list versus the new one but observing in debug after the button click I find that my list "CurrentList" is now empty. Why might the list, which is outside any method, be effected?

Thanks for the help understanding

Upvotes: 1

Views: 3102

Answers (4)

Tim Schmelter
Tim Schmelter

Reputation: 460138

All page's objects will be disposed at the end of the page's life-cycle. So you need to create and fill your list on every postback (or store it in Session what i wouldn't recommend).

You could use page's PreRender event to ensure that all events are already triggered:

protected override void OnPreRender(EventArgs e)
{
    // why do you need a list as field variable at all? I assume a local variable is fine
    List<int> CurrentList = new List<int>();
    foreach(var currentItemId in lvShareWithPanel.Items)
    {
        System.Web.UI.WebControls.DataKey currentDataKey = lvShareWithPanel.DataKeys[currentItemId.DataItemIndex];
        int FriendId = Convert.ToInt32(currentDataKey["SharedUserId"]);
        CurrentList.Add(FriendId);
    }
    // do something with the list
}

Note that you should not make it static as someone commented. That means you would use the same "instance" for every user and every request.

Here you can see all events:

enter image description here

Upvotes: 2

James
James

Reputation: 82096

ASP.NET is stateless, therefore the data is lost during Postback. You need to keep track of your CurrentList manually e.g. in the Session/ViewState.

public List<int> CurrentList
{
    get
    {
        return (List<int>)Session["CurrentList"] ?? new List<int>();
    }
    set
    {
        Session["CurrentList"] = value;
    }
}

Upvotes: 2

mihirj
mihirj

Reputation: 1219

You can store the list into ViewState and in PageLoad event, assign the List stored in ViewState to your class level List. This happens because of the Page Life Cycle where objects are disposed.

Upvotes: 0

Daniel Casserly
Daniel Casserly

Reputation: 3500

The list has no state value. So you need to store the list in either ViewState, Session state or otherwise.

Every ASP.NET page loses its values between page loads and only gets them back from state or if you enter them every time. Most controls store values in ViewState, which is page specific. This link should help.

Upvotes: 3

Related Questions