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
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.