Monday, March 26, 2012

Maintain Datatable values during postbacks

My page has a Listbox called Rooms, a text field called chairPerson and
a button called Add.

When Add is clicked, the script needs to display the selected info in a
datagrid.

For example, if the user selects Rooms 1,5 and 7, and enters Bijoy for
chairperson and clicks Add, a datagrid on the page should display the
following:

1, Bijoy
5, Bijoy
7, Bijoy

THEN if the user select Rooms 2 and 3, sets chairperson to Jeff and
clicks Add, the datagrid should display:

1, Bijoy
5, Bijoy
7, Bijoy
2, Jeff
3, Jeff

To accomplish this, I am using a DataTable. When Add is clicked, I add
"rows" to the DataTable, then bind the datagrid to this DataTable.

It works fine, except for a "memory loss" problem - meaning, it forgets
what was originally in the datagrid.

How can I use ViewState to save the DataTable across postbacks?

Thanks in advance.

BIjoyBijoy,

You'll need to persist the datatable by storing the object somewhere.

If it won't get too long I would place the datatable inside of a dataset
(which is serializable) and store the dataset in viewstate. Then whenever
you are adding rows to your datatable first get the dataset back out of
viewstate, add the rows, and then put it back into viewstate again.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"bijoy" <b_naick@.yahoo.ca> wrote in message
news:1126274773.035011.36680@.z14g2000cwz.googlegro ups.com...
> My page has a Listbox called Rooms, a text field called chairPerson and
> a button called Add.
> When Add is clicked, the script needs to display the selected info in a
> datagrid.
> For example, if the user selects Rooms 1,5 and 7, and enters Bijoy for
> chairperson and clicks Add, a datagrid on the page should display the
> following:
> 1, Bijoy
> 5, Bijoy
> 7, Bijoy
> THEN if the user select Rooms 2 and 3, sets chairperson to Jeff and
> clicks Add, the datagrid should display:
> 1, Bijoy
> 5, Bijoy
> 7, Bijoy
> 2, Jeff
> 3, Jeff
> To accomplish this, I am using a DataTable. When Add is clicked, I add
> "rows" to the DataTable, then bind the datagrid to this DataTable.
> It works fine, except for a "memory loss" problem - meaning, it forgets
> what was originally in the datagrid.
> How can I use ViewState to save the DataTable across postbacks?
> Thanks in advance.
> BIjoy
Actually I was able to add the datatable to the viewstate, and retrive
it after every postback, add new rows to it, then bind, then add to the
viewstate.
Bijoy,

Great! (you must be using Visual Studio 2005?) If so that accounts for the
difference, in the latest version datatables are serializable. They weren't
before.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"bijoy" <b_naick@.yahoo.ca> wrote in message
news:1126278539.599869.300920@.g44g2000cwa.googlegr oups.com...
> Actually I was able to add the datatable to the viewstate, and retrive
> it after every postback, add new rows to it, then bind, then add to the
> viewstate.
Nope. using VS 2003. The add/retrieve database from ViewState works
fine.

0 comments:

Post a Comment