这是一个Asp.net/Ajax错误吗? JavaScript错误和对象数据源 [英] Is this an Asp.net/Ajax bug? Javascript errors and the Object Data Source

查看:141
本文介绍了这是一个Asp.net/Ajax错误吗? JavaScript错误和对象数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个新的Web应用程序(我使用Visual Studio 2008版本9.0.30729.1 SP)

Create a new web application (I am using Visual Studio 2008 Version 9.0.30729.1 SP)

在aspx页面,用这种替换形式标签:(可能需要更改的类型名称来匹配您的页面名称)

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;
}

现在运行应用程序。我得到SYS是未定义脚本错误。自动脚本的大部分是完全缺失的。

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.

这是怎么回事呢?

推荐答案

我怀疑在这里发生的是,ObjectDataSource控件获取页面完成之前处理。

What I suspect happens here is that the ObjectDataSource gets disposed before the page finishes.

该ObjectDisposing事件的业务对象的实例(业务对象是在这种情况下您的页)被丢弃之前一直提出的。如果业务对象实现IDisposable接口,引发此事件后调用Dispose方法(页面实现IDisposable例如控制> TemplateControl>页

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

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.objectdisposing%28v=vs.80%29.aspx\">http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.objectdisposing(v=vs.80).aspx

您需要通过onobjectdisposing事件如取消对象的处置。

You need to cancel the disposing of the object via the onobjectdisposing event eg.

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Data" TypeName="WebApplication1.WebForm2"
        OnObjectCreating="ObjectDataSource1_ObjectCreating" 
        onobjectdisposing="ObjectDataSource1_ObjectDisposing"></asp:ObjectDataSource>

的处理程序:

    protected void ObjectDataSource1_ObjectDisposing(object sender, ObjectDataSourceDisposingEventArgs e)
{
    e.Cancel = true;
} 

有趣的设计有关系吗?一般来说,我preFER把我的ObjectDataSource方法在一个单独的类等。

Interesting design though? Generally I prefer to place my objectdatasource methods in a seperate class etc..

这篇关于这是一个Asp.net/Ajax错误吗? JavaScript错误和对象数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆