The problem with DataBind

N

nicklang

Say you are creating a page which contains a child control which uses a
DataList or DataGrid control. In a DataList, the rows are dynamic
controls and are data bound, so you have to first load the data source
and then DataBind. The problem arises when the containing Page calls
DataBind before your child control, say in their Page_Load. So you
have a Page with Page_Load calling DataBind and a child control with a
Page_Load and a DataBind.

What happens, I have found, is that on a PostBack, the DataBind is
broadcasted to your child control just after the ViewState is loaded
but before we get to start the child control's Page_Load. Thus, the
DataBind causes the child control to disappear because we bound before
we got to execute the event handler for the PostBack.

Does this make sense? I have solved this problem by overriding
DataBind, but it made me think. Can't all child controls be messed-up
by this problem of a DataBind in a parent control or page? As such,
should all controls override DataBind?

Thanks for your thoughts!
NL
 

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,770
Messages
2,569,584
Members
45,079
Latest member
ElidaWarin

Latest Threads

Top