I get this exception in my code and wonder if anyone can help me.
I have a Repeater Control attached to an ObjectDataSource, and the itemtemplate for the repeater contains a user control (ASCX). This user control, in turn, contains several other controls, mainly a GridView, associated with an ObjectDataSource.
When using the controls for the first time, everything works fine in this setting - the data is displayed correctly. However, when I change the filter parameter (drop-down lists outside the relay) and then re-bind the relay, I get an exception:
The ObjectDataSource 'expDataSource' control does not have a naming container. Make sure the control is added to the page before calling the DataBind. "in System.Web.UI.WebControls.DataBoundControlHelper.FindControl (Control control, String controlID) ... ... in System.Web.UI.WebControls.ObjectDataSource.LoadCompleteEventHandler (object sender, EventArgs e)
I'm not sure what the problem is: I read in several places that moving a data source outside the ASCX control can help - it does nothing. The object data source looks properly structured, and, as I said, it works for the first time (only).
I noticed an exception in the stack trace that occurs when ASP.NET calls FindControl () after LoadComplete () occurs. If I go through my code, it seems that all my code is finished before this happens, so this is all “system” code.
Why can't ASP.NET find this data source control in the LoadComplete handler?
Thanks!
Other notes:
This error occurs every time. Thus, the first time the data is loaded correctly, an error occurs during the second update with this error. Click "Download" again, it works (the third time).
While it fails, it looks like "Page_Load" is called twice in the ASCX control. So the templates are:
- Page_Load on the parent page
- Page_Load on ASCX
Downloading fine data
- Failure:
Load_page on parent page Load_page on ASCX Load_page on ASCX Exception
This comes from calling "Repeater.DataBind ()", but it behaves differently depending on whether it was already connected or not (obviously).
Additional notes:
Real weird behavior. I removed the SelectParameters list from the bottom of the ObjectDataSource, and unexpectedly the page does not reject the ObjectDataSource as having no NamingContainer. Of course, without these parameters, data binding will not actually work ... I can add them to the code, but why does it matter?