这是 Asp.net/Ajax 错误吗? JavaScript 错误和对象数据源
创建一个新的 Web 应用程序(我使用的是 Visual Studio 2008 版本 9.0.30729.1 SP)
在 Aspx 页面中,将 Form 标记替换为:(可能需要更改类型名称以匹配您的页面名称)
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" />
<div>
<asp:DropDownList runat="server" DataSourceID="ObjectDataSource1">
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Data" TypeName="WebApplication1.WebForm2"
OnObjectCreating="ObjectDataSource1_ObjectCreating"></asp:ObjectDataSource>
</div>
</form>
在服务器页面上,添加这个函数:
public IEnumerable<string> Data()
{
return new string[] { "some data", "foo", "bar" };
}
然后添加这个事件处理程序:
protected void ObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = this;
}
现在运行应用程序。我收到“Sys 未定义”脚本错误。自动脚本的大部分完全丢失。
现在注释掉对象实例行,
protected void ObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
//e.ObjectInstance = this;
}
现在当您运行应用程序时,不会出现脚本错误。
这是怎么回事?
Create a new web application (I am using Visual Studio 2008 Version 9.0.30729.1 SP)
In the Aspx page, replace the Form tags with this: (May need to change the type name to match your page name)
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" />
<div>
<asp:DropDownList runat="server" DataSourceID="ObjectDataSource1">
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Data" TypeName="WebApplication1.WebForm2"
OnObjectCreating="ObjectDataSource1_ObjectCreating"></asp:ObjectDataSource>
</div>
</form>
On the server page, add this function:
public IEnumerable<string> Data()
{
return new string[] { "some data", "foo", "bar" };
}
And then add this event handler:
protected void ObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = this;
}
Now run the application. I get "Sys is undefined" scripting errors. Large portions of the automatic script is completely missing.
Now in comment out the Object Instance line,
protected void ObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
//e.ObjectInstance = this;
}
Now when you run the application, there are no script errors.
What is going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑这里发生的是 ObjectDataSource 在页面完成之前被释放。
ObjectDispose 事件始终在业务对象实例(在此上下文中业务对象是您的页面)被丢弃之前引发。如果业务对象实现 IDisposable 接口,则在引发此事件后调用 Dispose 方法(页面实现 IDisposable,例如 Control>TemplateControl>Page
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols .objectdatasource.objectdispose(v=vs.80).aspx
您需要通过 onobjectdisusing 事件取消对象的处置,例如。
接待员:
但是设计很有趣吗?一般来说,我更喜欢将 objectdatasource 方法放在单独的类中等。
What I suspect happens here is that the ObjectDataSource gets disposed before the page finishes.
The ObjectDisposing event is always raised before the instance of the business object (business object being your page in this context) is discarded. If the business object implements the IDisposable interface, the Dispose method is called after this event is raised (page implements IDisposable e.g. Control>TemplateControl>Page
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.objectdisposing(v=vs.80).aspx
You need to cancel the disposing of the object via the onobjectdisposing event eg.
The handler:
Interesting design though? Generally I prefer to place my objectdatasource methods in a seperate class etc..