Is this an Asp.net/Ajax bug? Javascript errors and the Object Data Source
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 err开发者_如何学JAVAors. 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?
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..
精彩评论