Where in the viewstate is a GridView's rows?

K

Keith Patrick

I'm trying to track down a problem where a dynamic gridview I have is not
restoring its rows correctly. Stepping into a derived GridView, I've found
that the GV *is* getting a viewstate passed to LoadViewState with the
correct # of rows specified. However, I can't find where in the viewstate
(not on the datasource/datasourceview, either) the rows are being stored,
since the values are not being restored & I can't figure out how the items
are added back.
 
K

Keith Patrick

I never found where it was stored (I assumed it was in the
datasourcecontrol, but I couldn't see the data there), but basically, the
gridview restores the rows from either databinding or by viewstate via
CreateChildControls(IEnumerable, bool databinding). I was overriding
CreateChildControls() without calling base.CreateChildControls(), so this
method never got called, preventing my rows from being restored.
 
M

Mike MacMillan

Keith,
does the control have both SaveViewState and LoadViewState overridden
for custom handling? if so, SaveViewState will tell you what's being
saved and how, that way during LoadViewState you can put everything
back where it belongs. can you post some of the code perhaps?

thanks,
Mike MacMillan
 
K

Keith Patrick

I overrode them so I could see what the viewstates were, but I never changed
the viewstates in the gridview. The problem was that I was breaking the
CreateChildControls() method chain, preventing the rows being restored from
an IEnumerable that something passes into the GV (the specific method I
needed called was CreateChildControls(IEnumerable data, Boolean
isDataBinding) )
 
T

Teemu Keiski

It stores the count of rows when databinding, and on postback creates a
dummy data source (array, count from the count stored to ViewState) to
instantiate the rows back again. GridViewRows are controls themselves
(derive from TableRow), so basically when they are added back to the
GridView, they load their own state. So essentially what GridView handles
for this matter is to recreate the same count of GridViewRows.

The key points are PerformDataBinding and CreateChildControls methods, which
delegate row creation to overload of CreateChildControls taking the data
source as IEnumerable and a boolean flag to tell if databinding or restoring
from ViewState
 

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

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top