简体   繁体   English

ASP.NET DataSource控件“没有命名容器”异常

[英]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). 我有一个绑定到ObjectDataSource的Repeater控件,并且转发器的itemtemplate包含一个用户控件(ASCX)。 This user control in turn contains several other controls, mainly a GridView that is associated with an ObjectDataSource. 该用户控件又包含几个其他控件,主要是与ObjectDataSource关联的GridView。

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: 但是,当我更改过滤器选项(转发器外部的下拉菜单),然后重新绑定Repeater时,我得到异常:

The ObjectDataSource control 'expDataSource' does not have a naming container. ObjectDataSource控件'expDataSource'没有命名容器。 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) 确保在调用DataBind之前将控件添加到页面中。“在System.Web.UI.Web.控件,控件控件,字符串控件ID中的System.Web.UI.WebControls.DataBoundControlHelper.FindControl(控件控件,字符串控件ID)......”...在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. 我不确定问题是什么 - 我在一些地方读过,将数据源移到ASCX控件之外可能会有所帮助 - 这没什么用。 The objectdatasource appears to be properly structured, and as I said, it works the first time (only). objectdatasource看起来结构合理,正如我所说,它是第一次(仅)。

I noticed in the stack trace for the exception that this is occurring when ASP.NET is calling FindControl() after LoadComplete() occurs. 我在堆栈跟踪中注意到,当发生LoadComplete()之后ASP.NET正在调用FindControl()时发生这种情况。 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? 为什么ASP.NET无法在LoadComplete处理程序中找到此数据源控件?

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. 在它失败的时候,看起来在ASCX控件中调用了两次“Page_Load”。 So the patterns are: 所以模式是:

    1. Working Pattern: 工作模式:
  • Page_Load on Parent Page 父页面上的Page_Load
  • Page_Load on ASCX ASCX上的Page_Load
  • Data Loads fine 数据加载正常

    1. Failing Pattern: 失败模式:
  • Page_Load on Parent Page 父页面上的Page_Load
  • Page_Load on ASCX ASCX上的Page_Load
  • Page_Load on ASCX ASCX上的Page_Load
  • 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). 这一切都发生在对“Repeater.DataBind()”的调用中,但它的行为会有所不同,具体取决于它是否已被绑定(显然)。

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. 我从ObjectDataSource的底部删除了SelectParameters列表,突然之间,该页面没有拒绝ObjectDataSource,因为没有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 DropDownFilter

Repeater 中继器

UserControl X UserControl X.

ObjectDataSource ObjectDataSource控件

ControlParameters Referencing DropDownFilter ControlParameters引用DropDownFilter

End ObjectDataSource 结束ObjectDataSource

End UserControl X 最终用户控件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. 正如您所看到的,在Repeater中,ItemTemplate是用户控件,而后者又拥有带有ControlParameters的“有罪”ObjectDataSource。 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). 这些控制参数在引用的父页面上具有DropDownList过滤器的名称(所以基本上,如果将此控件添加到任何其他页面,如果找不到具有正确名称的控件,它当然会失败)。

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. 因此,当我通过并将所有ControlParameters更改为Parameters(删除对该DropDownList控件的引用)时,现在我不再收到错误。

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(). 我可以假设的是,这个数据源在父页面上引用了一个控件这一事实意味着它很难被添加回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. 这是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. 要在ItemTemplate中显示项目,我们应该使用LayoutTemplate中的服务器控件作为ItemTemplate的占位符。 For example, we could use a Table/Div control with an ID Property in Layout Template. 例如,我们可以在布局模板中使用带有ID属性的Table / Div控件。 At run time, this placeholder control will be replaced with the contents of the ItemTemplate and "naming container error" will be disappeared. 在运行时,此占位符控件将替换为ItemTemplate的内容,“命名容器错误”将消失。 Finally, if you are having an objectDataSource in ItemTemplate, make sure that you added somthing(like table/Div) with "Id" property in Layout Template. 最后,如果你在ItemTemplate中有一个objectDataSource,请确保在布局模板中添加了带有“Id”属性的somthing(如table / Div)。

Thanks, Sunil. 谢谢,苏尼尔。

Ray hit the nail on the head. 雷击中了头上的钉子。 You are definitely missing an "if(!IsPostBack)" somewhere. 你肯定错过了“if(!IsPostBack)”某个地方。 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. 你说它在ItemTemplate中,所以可能不是......但是对Page_Load的多次调用意味着控件的多个副本。

Use both DataBind. 同时使用DataBind。 Example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM