Here is what is in my code-behind:
List<Event> events = new List<Event>();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void AddEvent_Click(object sender, EventArgs e)
{
Event ev = new Event();
ev.Name = txtName.Text;
events.Add(ev);
}
I want to add an item to the list every time the Add button is clicked, but the list is reset after every postback. How can I keep the data in the list between postbacks?
I often use a technique such as this, although keep in mind this can cause your viewstate (as rendered to the browser) to grow rather large:
Then when you want to use the list you would do something like this:
Also note that your Event class will need to be serializable, but that’s usually as simple as adding the [Serializable] attribute to the class (unless its a really complex class).