CodeMan5000
CodeMan5000

Reputation: 1087

Retrieving object List in Asp.net

I have a list of class object that I created as a variable in my deafault.aspx.cs page

List<BoldGauge> boldGauges = new List<BoldGauge>();

I create my object in nessacary function and then add the newly created object to the list so I can retrieve it later as needed.

When I attempt loop through the object later the boldGagues count = 0. I assume I need to either add the List to a session variable or session state.

Does anyone know the best approach for this? There could be numerous different types of controls in multiple lists, so if someone could please recommend an approach that is least expensive, and efficient I would appreciate it.

Upvotes: 2

Views: 598

Answers (2)

Jaime Yule
Jaime Yule

Reputation: 1081

foreach(var boldGaug in boldGauges)
{
    //do something..
}

Upvotes: 0

Kyle Trauberman
Kyle Trauberman

Reputation: 25694

To persist the list across postbacks, you'll need to store the list somewhere.

You can store it in the Session, but a more applicable place might be the ViewState.

ViewState.Add("GaugesList", boldGauges);

then get it back later

List<BoldGauges> boldGauges = ViewState["GaugesList"];

Note that this is scoped to the page, so if you need the list across pages, use the Session.

Upvotes: 3

Related Questions