Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

ListBox vs Table Server Control Persisting

I have a screen where I want a user to enter a data into rows/columns added dynamically.

When I have a table server control on the page - and I add rows into it dynamically. Dynamically added rows will disappear when the page gets posted back.

If I do the same with a list box server control - I add listItems into it dynamically, but when the page gets posted back the rows are still there !

So can someone explain why a listbox will persist its dynamically added contents where as a table will not - they are both asp server controls.

Thanks !!

Wednesday, May 14, 2003

Listboxes are end up being returned to the browser as a standard HTML <select>. <select>, <input> and <textarea> tags are standard HTML "widgets" whose sole purpose in life is to send data to web servers for further processing. Hence *all* browsers will automatically send the data contained in these controls to the server. Anything else in the client, however it was created, will *not* be sent to the server automatically. Hence your table control, although it ends up being sent to the browser as a standard HTML <table> is always going to be ignored by the browser when it comes to submitting the data to the server.

Saying that, ASP.NET does have a little trick up it sleeve whereby server controls can appear to "remember" their state when you switch on "runat=server". It does this by storing data in a hidden field called ViewState: <input name='__VIEWSTATE' type='hidden' value='....'> This allows you to do things such as add a runat=server to a standard <p> tag and it automatically become stateful - for example it'll "remember" a programmatically set InnerText through PostBacks.

But for some reason (likely performance and being concious of potentially large ViewState size) MS decided not to store the layout of <asp:Table> in ViewState.

If you want you could programmatically loop through the tables and rows of the table and something to represent it in ViewState. Most things marked as "Serializable" can be stored there - but do keep your eye on its size.

Duncan Smart
Wednesday, May 14, 2003

*  Recent Topics

*  Fog Creek Home