Looping over a DataGrid after postback; questions

  • Thread starter Thread starter Jim Bancroft
  • Start date Start date
J

Jim Bancroft

This may be a no-brainer, but I'm sure I follow what's happening here...

I have a DataGrid with one DropDownList per row. If I select a few DropDown
items and postback my page, I can't loop through the DataGridItems unless I
rebind my DataGrid. By "can't loop," I mean it's a mighty quick run
through-- zero iterations.

I was wondering...do I have to rebind on postbacks? If so, what happens if
I bind the DataGrid to a different set of data (same datasource and table
let's pretend, but different rows returned) on the postback? I tried it and
my DropDowns looked pretty... strange afterwards. Duplicates and such.
Hard to describe but it was surreal.

In scouring my brain I think there are some caveats about databinding on
postbacks? I could be wrong but are there specific gotchas I should be
aware of? Thanks for your help.
 
There is no need to databind your datagrid to iterate it. The datagrid will
be maintained through viewstate with default settings (viewstate enabled).
And hence you can iterate it.

Post your code for more specific details.
 
Thanks-- I don't have the page viewstate enabled, in fact. I didn't want
the overhead. Oops.

If I were to enable the viewstate, what would happen if I rebound my
DataGrid after a postback? I assume that action would "overrule" the
viewstate's copy of the data, and I'd be looping through the new data?

I ask because my goal is to submit the form, loop through all the DropDowns
in the DataGrid, update my datasource, then reload the DataGrid from that
datasource. I'm not sure what the best sequence of events would be to
accomplish this. If anyone knows I'd be grateful. Thanks again.

-Jim
 
Back
Top