I have a problem and I have consulted some other people with this but they too didn't know what the problem was. I have a very simple asp.net page right now with some table controls on there. The page consists of a form in which objects can be added (which is not implemented jet). And There are buttons beneath the tables which clears the objects from these tables. All objects are shared in a single list and have an enum which seperates them from tables.
//fields List animals;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
animals = new List<Animal>();
animals.Add(new Animal("kees", false, species.dog) { Age = 13, RegNr = "0123456789" });
animals.Add(new Animal("Henk", true, species.dog));
animals.Add(new Animal("Cat", false, species.cat));
ViewState["animalsSession"] = animals;
fillTables();
}
}
private void fillTables()
{
foreach (Animal a in (List<Animal>)ViewState["animalsSession"])
{
//make a row
TableRow newRow = new TableRow();
//making cells
TableCell cellName = new TableCell();
cellName.Text = a.Name;
newRow.Cells.Add(cellName);
TableCell cellAge = new TableCell();
cellAge.Text = (a.Age == -1 ? String.Empty : a.Age.ToString());
newRow.Cells.Add(cellAge);
TableCell cellRegNr = new TableCell();
cellRegNr.Text = (a.RegNr == null ? String.Empty : a.RegNr);
newRow.Cells.Add(cellRegNr);
TableCell cellReserved = new TableCell();
cellReserved.Text = (a.IsReserved ? "x" : String.Empty);
newRow.Cells.Add(cellReserved);
if (a.Species == species.dog)
{
dogTable.Rows.Add(newRow);
}
else if (a.Species == species.cat)
{
catTable.Rows.Add(newRow);
}
}
}
protected void dogDeleteButton_Click(object sender, EventArgs e)
{
List<Animal> cats = new List<Animal>();
foreach (Animal a in (List<Animal>)ViewState["animalsSession"])
{
if (a.Species == species.cat)
{
cats.Add(a);
}
}
ViewState["animalsSession"] = cats;
fillTables();
}
Now this all works as expected, but notice that the aniimalSession is not a session but a viewstate. When I change all viewstates on this page with sessions it's not working at all. I don't get why this is since how I learned it they do kinda the same. Only the session saves the information on the server while the viewstate saves it on the client, but that shouldn't prevent the session from not working in this case right?
Does somebody know what I did wrong here, or are there other differences in sessions and viewstate.
BTW I know I could better use something like a gridview, but I'm learning about sessions right now so I try to get it working using these tables.
Can you please make sure whether session is enabled or not. Please refer to the link below to check whether session state is enabled or not at the ASP Alliance.