Lost ViewState :: GridView + ObjectDataSource

J

Julio César

Hi all!

Is there a way to prevent a GridView's databinding in order to lose its
viewstate? (I don't want the users to retype everything again in the
gridview within each postback when user-input is invalid)

What I understand about these controls is:

[GridView]
a. Assuming it's the very first time the page is requested
1. If EnableViewState = false, then GridView databinds (1st
databind)
b. Then the view renders to the web-browser (after all events of the
ASP.NET 2.0 page life-cycle are finished)
c. User interacts with the gridview and inputs some values. Then submits
the form. A postback is done.
d. When postback, Step 'a' is accomplished to get the original values
from the database by means of the ObjectDataSource's "Select Method".
e. Then according to ASP.NET Page Life-Cycle, the postback values (what
the user typed) are loaded into the GridView control.
f. Then Page_Load() event is raised.
g. Then GridView/ObjectDataSource post-back events are raised, for
example:

protected void odsPersonas_Updating(object sender,
ObjectDataSourceMethodEventArgs e)
{
//Validate input by means of ObjectDataSource's
e.InputParameters
//Maybe input is NOT VALID....so I can cancel this update by:
e.Cancel = true;
}

h. Then GridView AUTOMATICALLY databinds and THAT'S THE PROBLEM: it's
overwriting the data from step "e" and original values from the database are
loaded into the GridView. (that's the 2nd databind...which is normal and
correct because after an update to the database, we want to bring back those
records with the new values from step 'e')
i: ASP.NET 2.0 Page Life-Cycle finishes.
j. The view renders.
k. The state of the GridView (with the original invalid input from the
user) is LOST! How can I prevent this????

P.D.
I have done the same with a FormView and I'm able to prevent the 2nd
databind from step 'h' by handling the "FormView_ModeChanging(...)" event
and it works fine: the state of the control (with the user's invalid data)
is preserverd.

How can I accomplish the same behaviour using the GridView??

Best regards,

Julio César.
 
J

Julio César

OK! I think I have it...

I have to manage the GridView_RowUpdating() event BEFORE the
ObjectDataSource_Updating() event...

In each case, I can access a map with the values that the user typed
(without manually searching the values by means of FindControl(...)) and if
I cancel the event at the GridView_RowUpdating() point, then the viewstate
is preserved!. And in fact that 2nd databind is prevented which is the
guilty guy that causes the lost of the state of the form.

Well, we always learn something...

Thank you!
 

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,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top