views:

48

answers:

3

I have a grid with one TemplateField which is a checkbox and say 2 bound fields.

In Page_Load on postback I rebind the grid with cached dataset stored in session. If user selects checkbox on the grid, upon postback i can iterate on the grid and get those checked values, everything works fine.

If I move the code of grid binding on postback to OnInit instead of Page_Load then i loose those user checked checkbox data. Why? Should the postback data not overlay on top of the grid after oninit?

It does work with page_load, I'm assuming that when i rebind the grid, I'm overlaying my data on grid which has postback data, since the checkbox column is not bound i do not overwrite postback data.

NOTE: viewstate is disabled on my grid, i bind data (stored in session) on everyback postback. Also something weird, when I'm using OnInit, on every postback i get the first page of the grid, no matter which page the postback was triggered. Thanks.

+1  A: 

I believe this is because the viewstate isn't loaded yet in the OnInit event. Check this out http://msdn.microsoft.com/en-us/library/ms178472.aspx for more on the asp.net page lifecycle. Hope it helps!!

Mike
ViewState is disabled on my grid, Thats the reason I bind data stored in session on every postback.
Upvote for referencing the the ASP.NET lifecycle - VERY useful page.
mikemanne
A: 

Similar to LoadViewState, ProcessPostData is something that happens after OnInit, but before Page_Load. I assume that the checkbox values are shipped back to the server as Post data. Thus I suspect the problem is the same as if ViewState was enabled: OnInit is too early - the posted data (checkbox values) hasn't yet been applied to your controls.

I don't have extensive experience with viewstate-disabled operation, so my apologies if this answer is missing some nuances.

mikemanne
I agree with you, postdata is not there for OnInit and that is OK. I'm iterating over the grid on OnPageIndexChanging and there I expect the postdata but do not get. Just because I bound the grid in OnInit changes behaviour. I would like to understand the difference between binding grid on Page_Load Vs OnInit. There is something I'm missing.
It's possible that re-binding the grid in OnInit makes it impossible for the ProcessPostData to overwrite the correct data elements with the checkbox values. But that's speculation on my part - I'm not positive. Sorry - I think my lack of experience with viewstate-less controls is making it tough for me to be very helpful. :-/
mikemanne
A: 

Someone answered it on asp.net forum. Here is the link

http://forums.asp.net/p/1592192/4036031.aspx#4036031