ASP.NET DataSource Control "has no naming exception" exception

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:

    • Work pattern:
  • Page_Load on the parent page
  • Page_Load on ASCX
  • Downloading fine data

    1. 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?

+4
source share
4 answers

Found a strange solution that I will post, and we can discuss to understand why this is fixed.

On my page, I had the following structure (rephrasing the tags somewhat):

Page

DropDownFilter

Repeater

UserControl X

ObjectDataSource

ControlParameters Link to DropDownFilter

End ObjectDataSource

End UserControl X

Repeater end

Final page

So, as you can see, there was a user control in the Repeater ItemTemplate, which, in turn, possessed the “guilty” ObjectDataSource using ControlParameters. These controls had the name of the DropDownList filter on the specified parent page (so basically, if this control was added to any other page, it would certainly fail if it could not find the control with the corresponding name).

So, when I went through and changed all the ControlParameters to Parameters (removed the link to this DropDownList control), now I no longer get the error.

All I can assume is that the fact that this data source was referencing a control on the parent page meant that it was having problems adding the page set to DataBind () back to the control. You would think that this would be unsuccessful for the first time, if he failed at all, so it still remains a mystery.

Any thoughts?

+2
source

This is an exceptional error in ASP.NET DataControls. I had a similar problem, and I lost a few months for this eccentric error, but finally got a solution. The reason is To display items in an ItemTemplate, we must use the server control in a LayoutTemplate to act as a placeholder for an ItemTemplate. For example, we could use the Table / Div control with the Property identifier in the template template. At run time, this placeholder control will be replaced with the contents of the ItemTemplate, and the "name container error" will disappear. Finally, if you have an objectDataSource in an ItemTemplate, make sure you add somthing (like table / div) with the "Id" property in the layout template.

Thank you, Sunil.

+2
source
Rap hit the nail on the head. You are definitely missing "if (! IsPostBack)" somewhere. How do you add a custom control to the repeater? Is it dynamic? You say this in an ItemTemplate, so probably not ... But multiple calls to Page_Load involve multiple copies of the control.
+1
source

Use both DataBind. Example:

SqlDataSource1.DataBind(); ListView1.DataBind(); 
+1
source

All Articles