By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,116 Members | 1,509 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

EnableViewState not working on table

P: n/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?????
Nov 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

"AFN" wrote:
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?????

Nov 19 '05 #2

P: n/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.
"vinay" <vi***@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
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.

"AFN" wrote:
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?????

Nov 19 '05 #3

P: n/a
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).
"AFN" <ne***************************@DELETETHISyahoo.com > wrote in message
news:8B*******************@twister.socal.rr.com...
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?????

Nov 19 '05 #4

P: n/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.
"David Jessee" <dj*****@houston.rr.com> wrote in message
news:ec****************@TK2MSFTNGP10.phx.gbl...
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).
"AFN" <ne***************************@DELETETHISyahoo.com > wrote in message
news:8B*******************@twister.socal.rr.com...
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?????


Nov 19 '05 #5

P: n/a
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),TableStructureDes criptor)
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

"AFN" <ne***************************@DELETETHISyahoo.com > wrote in message
news:16*******************@twister.socal.rr.com...
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.
"David Jessee" <dj*****@houston.rr.com> wrote in message
news:ec****************@TK2MSFTNGP10.phx.gbl...
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).
"AFN" <ne***************************@DELETETHISyahoo.com > wrote in message
news:8B*******************@twister.socal.rr.com...
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?????



Nov 19 '05 #6

P: n/a
AFN
Thanks for all the helpful sample code and idea.
"David Jessee" <dj*****@houston.rr.com> wrote in message
news:uA****************@tk2msftngp13.phx.gbl...
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),TableStructureDes criptor)
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

"AFN" <ne***************************@DELETETHISyahoo.com > wrote in message
news:16*******************@twister.socal.rr.com...
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.
"David Jessee" <dj*****@houston.rr.com> wrote in message
news:ec****************@TK2MSFTNGP10.phx.gbl...
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).
"AFN" <ne***************************@DELETETHISyahoo.com > wrote in message news:8B*******************@twister.socal.rr.com...
> 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?????
>
>



Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.