EnableViewState not working on table

A

AFN

I have a server-side table,

<asp:table id="tblData" EnableViewState="true" runat="server" />

and my code-behind adds rows of data to the table in code that says:

if not page.ispostback then
LoadDataTable()
end if

I also have a checkbox, with autopostback=true.

On the first page_load, the data table has rows of data. The problem is
that when I check the checkbox and the page posts back, the table
disappears. I don't understand why it doesn't preserve the table
considering EnableViewState is true?????
 
G

Guest

Try reloading the LoadDataTable() on postback.
I dont think viewstate will work for Table.
It is also similar behaviour when you create the controls dynamically.
Need to reload them on postback.
 
A

AFN

I know that reloading the table data will work, but that's so wasteful.
The table element shows EnableViewState in its properties in the designer,
so I don't see why it wouldn't work.
 
D

David Jessee

The Table's Viewstate maintains property changes on the table control (e.g.
Width, Height).
The problem is that the rows/cells you're adding are different controls.
The table would not keep track of those other controls (think of it as a
panel that you're dynamically adding textboxes to).
 
A

AFN

Good point, but now what? All I'm doing is adding rows and cells. How can
I make those part of the viewstate? I really don't want to call the
database twice. I actually do about 4 database calls to produce the data
and it would be a huge waste to do that again. Maybe caching will work (I
don't know if caching will work on postback caused by the checkbox being
checked, which triggers the server-side exposing of a panel that is
otherwise invisible), but I'd prefer to just put the table rows/cells in
viewstate.
 
D

David Jessee

You can't. Controls are not serializable so they can't be asses to the
viewstate.
Save whatever information you need to in the viewstate, then rebuild the
table during the page's Init event.

as far as the database calle that get the data for the table
construction...create a class that represents the data you need. Remember
that you can have properties that are arraylists so whatever you need to
store in that class, you can store. Make sure that the class is marked as
serializable, then save THAT in your viewstate. (Even better yet, instead
of putting all of that logic in your page, make your Data Access Class
return that class)

<Serializable()> Public Class TableStructureDescriptor
....your implementation
End Class

then in your aspx code behind......

Public Class MyPage
Inherits Page

Private Const vs_theStructure as string = "TableStructure"

Public Property TableStructure as TableStructureDescriptor
Get
if Viewstate(vs_theStructure) is nothing then return nothing
return
CType(Viewstate(vs_theStructure),TableStructureDescriptor)
End Get
Set(Value as TableStructureDescriptor)
Viewstate(vs_theStructure)=Value
End Set
End Property

Private Page_Init()
If Not TableStructure is nothing then
BuildTable()
End if
End Sub

End Class


End Class
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top