0
votes

I'm having some trouble with viewstate, perhaps I don't understand how it works.

I have a table and add rows to it dynamically, but when I try to add the rows to the viewstate, the rows are not displayed (added?) in the table.

private void randomPageMethod()

{

 ...get data & generate row...
 ViewState.Add(tr.ID, tr);
 tableLSHTime.Rows.AddAt(1, tr);

}

If I remove the ViewState.Add(tr.ID, tr) line,the rows are added, but ofc not preserved.

I would like to use Page_Load like this (when I use viewstate):

protected void Page_Load(object sender, EventArgs e)

{

  ...code code...
  if (IsPostBack)
  {
        foreach (TableRow tr in ViewState)
        {
            tableLSHTime.Rows.Add(tr);
        }
  }

}

1
Any reason not to use the build in data controls like GridView or ListView? - Eric Fan
No reason, its just bugging me like crazy. If I use Session instead of ViewState everything works. - Uros Stankovic

1 Answers

0
votes

Instead of adding rows directly to viewstate, use a datatable and add rows to it, and add the datatable to viewstate. If can be retrieved and looped through inside page_load handler.