FormView and GridView with DataSources



I read some msdn and other articles about how does databinding among
DataSource controls and FormView / GridView controls works but I still don't
fully understand to this blackbox. I have few questions and hopefully you can
provide me some answers.

1. Best place for DataBinding.
Where is the best place for calling DataBind method? Is it possible to say
don't bind controls for this postback and use current values instead? These
questions are mainly oriented on non-submit postbacks (like dependency
between two controls in formview, after selecting value in first one, second
one is updated so postback is needed) and for server side validations. I had
some problems with calling validations, populating and binding in proper

Small description of two problems I recently had:
FormView with many controls and validators - after save button is pushed
postback is processed. First in click event handler Page.IsValid is tested.
If page is invalid processing is completed and page has to return to user.
Before this Page_PreRender with FormView's DataBind is called. After this
call all validators become invisible. If I do not call FormView's DataBind,
binding is called automaticly when page is about to render. Only way how to
pass this problem was to validate page again after FormView's DataBind is

Second problem is probably very similar. I had FormView with other
populating postbacks. When populating postback or validation was called
during UpdateMode of FormView, all controls in FormView have been bound to
values from SelectMethod so all changes have been lost. To solve this problem
I put my bussiness object to session and updated datasource to use this
object. In every populating postback object in session was updated by new set
of values. This partial updates caused difficulties because validators for
controls had to be called manually to store every valid value.

2. Best place for storing business object used by ObjectDataSource and what
is best "pattern" for using ObjectDataSource
As you could see in previous example i am looking for best practice to use
FormView / ObjectDataSource controls. My first idea was to use SelectMethod
from session and Update, Delete, Insert method directly to DB. This looks
like odd sloution because previous examples do not work as I would like. So
how to used it in best way?

3. Inserting
This was special problem with FormView. I used InsertMode to create new
item. Is there any possibility how to store values between postbacks? My
soloution with object in Session used for UpdateMode didn't work for
InsertMode. SelectMethod didn't bind controls so after every postback all
controls (textboxes) were blank. My workaround was to store values from
controls to fields and populate them again after FormView's DataBind was
called in Page_PreRender. I realy don't like this solution but i don't have
any idea how to do it better.

I had some similar problems with GridView when i tryed to allow user to make
all changes like inserting, deleting and updating rows and save all changes
to DB after save button has been pushed.

Thanks for any responce / ideas,
Ladislav Mrnka

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

Latest member

Latest Threads