How to Persist Dynamic Rows within WebControls.Table?

S

samtilden

Team:

I have made a System.Web.UI.WebControls.Table called Table1. I can add
a few rows in the VS2005 Designer and those rows do indeed persist
between web page views. I can even do a Table1.Rows.Clear() and these
rows go away, as expected.

The problem is that when I add rows dynamically within my aspx.cs
source code (via Table1.Rows.Add(row), the added rows appear once and
only once. When I refresh the page, the rows I added dynamically are
gone from the web page.

I turned on EnableViewState=true for Table1, for every Row within
Table1, for every Cell within every Row, and for every Control within
every Cell. The dynamically added rows do not appear the second time,
after a refresh.

I have other web controls that work fine, e.g., CheckBoxList, that
persist their dynamically added data (in this case via Items.Add()).

What am I doing wrong?

Thanks.
 
A

Alessandro Zifiglio

hi, you must let the same code you used to add the dynamic rows re-execute
after a postback. You must know that after your page is rendered to the
client, it is torn down and destroyed, so a postback to your page will start
reconstructing all items from scratch. Initially when you had the rows added
via the vs.net designer, it adds the rows declaratively in your .aspx so it
is being rebuilt again, getting the information of the rows to construct
from there. Now that you are doing this dynamically, you should allow the
same code(the code that adds your dynamic rows) to re-execute after a
postback, and a bit early in the pages life cycle. Some obvious mistakes are
not to wrap that part of your code in an if ispostback check, and if you are
adding the rows in the click of a button, then make sure you set some flag
in viewstate, that way you can re-execute the same code for reconstructing
the rows again, based on a condition. This reconstuction wont make you lose
whatever state information that was already present before, since viewstate
will be reloaded back into the control in question after the reconstruction.
If you still have problems then post your source code.
Some good references for you on msdn :
http://msdn2.microsoft.com/en-us/library/hbdfdyh7.aspx
http://support.microsoft.com/kb/317794/EN-US/

Regards,
Alessandro Zifiglio
http://www.AsyncUI.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,113
Latest member
KetoBurn
Top