English 中文(简体)
ASP.NET DataSource Control "does not have a naming container" exception
原标题:

I ve been getting this exception in my code and wondered if anyone could help me out.

I have a Repeater Control bound 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 that is associated with an ObjectDataSource.

On initial use of the controls in this setup, everything works great - data displays properly. However, when I change a filter option (dropdowns outside of the repeater), and then rebind the Repeater, I get the exception:

The ObjectDataSource control expDataSource does not have a naming container. Ensure that the control is added to the page before calling DataBind." at System.Web.UI.WebControls.DataBoundControlHelper.FindControl(Control control, String controlID) ... ... at System.Web.UI.WebControls.ObjectDataSource.LoadCompleteEventHandler(Object sender, EventArgs e)

I m not sure what the problem is - I ve read in a few places that moving the datasource outside of the ASCX control might help - this does nothing. The objectdatasource appears to be properly structured, and as I said, it works the first time (only).

I noticed in the stack trace for the exception that this is occurring when ASP.NET is calling FindControl() after LoadComplete() occurs. If I step through my code, it appears as though all my code is finished executing before this happens, so it s all "system" code.

Why would ASP.NET not be able to find this datasource control in the LoadComplete Handler?

Thanks!

Other Notes:

  • This error occurs every other time. So the first time the data loads properly, then on second refresh fails with this error. Clicking "Load" again, it works (on the third time).

  • On the times that it fails, it looks like "Page_Load" is being called twice in the ASCX control. So the patterns are:

    1. Working Pattern:
  • Page_Load on Parent Page
  • Page_Load on ASCX
  • Data Loads fine

    1. Failing Pattern:
  • Page_Load on Parent Page
  • Page_Load on ASCX
  • Page_Load on ASCX
  • Exception

This is all happening from a call to "Repeater.DataBind()", but it behaves differently depending on if it has already been bound or not (evidently).

More Notes:

Real strange behavior. I removed the list of SelectParameters from the bottom of the ObjectDataSource, and all of a sudden, the page does not reject the ObjectDataSource as not having a NamingContainer. Of course, without these parameters, Databinding won t actually work...I can add them in the code, but why would it matter?

最佳回答

Found a strange solution, that I ll post and we can discuss to maybe figure out why this fixed it.

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

Page

DropDownFilter

Repeater

UserControl X

ObjectDataSource

ControlParameters Referencing DropDownFilter

End ObjectDataSource

End UserControl X

End Repeater

End Page

So as you can see, within the Repeater ItemTemplate was the user control, which in turn had the "guilty" ObjectDataSource with ControlParameters. These control parameters had the name of the DropDownList filter on the parent page referenced (so basically, if this control was added to any other page, it would of course fail if it couldn t find a control with the proper name).

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

All I can assume is that the fact that this datasource referenced a control on the parent page meant that it was having trouble getting added back to the page s control set on a DataBind(). You would have thought it would fail the first time if it was going to fail at all, so that s still a mystery.

Any thoughts?

问题回答

This is an exceptional error in ASP.NET DataControls. I had similar problem and lost few months behind this eccentric error, but finally got the solution. The reason is; To display items in ItemTemplate, we should use a server control in the LayoutTemplate to act as the placeholder for the ItemTemplate. For example, we could use a Table/Div control with an ID Property in Layout Template. At run time, this placeholder control will be replaced with the contents of the ItemTemplate and "naming container error" will be disappeared. Finally, if you are having an objectDataSource in ItemTemplate, make sure that you added somthing(like table/Div) with "Id" property in Layout Template.

Thanks, Sunil.

Ray hit the nail on the head. You are definitely missing an "if(!IsPostBack)" somewhere. How are you adding the user control to the repeater? Is it dynamic? You say it s in the ItemTemplate, so probably not... But multiple calls to Page_Load imply multiple copies of the control.

Use both DataBind. Example:

SqlDataSource1.DataBind();
ListView1.DataBind();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签