这是 Asp.net/Ajax 错误吗? JavaScript 错误和对象数据源

发布于 2024-10-14 11:55:20 字数 1256 浏览 3 评论 0原文

创建一个新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌味 2024-10-21 11:55:20

我怀疑这里发生的是 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 事件取消对象的处置,例如。

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

但是设计很有趣吗?一般来说,我更喜欢将 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.

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

The handler:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文