Sunday, December 18, 2011

How to solve "sys is undefined" error while using asp.net ajax update panel.

We have the error "sys is undefined" often when we use asp.net ajax update panel in our ajax enable web site.
The best remedy for this problem is to add the script to the ScriptManager’s Scripts collection and then call our wireups in a Sys.Application.init event handler. It is pretty good practice to separate .js file from source as much as we can.

In .aspx file,
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"
    EnablePartialRendering="true" EnablePageMethods="true">
        <Scripts>
            <asp:ScriptReference Path="Init.js" />
        </Scripts>
    </asp:ScriptManager>
    <br />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
           <%-- your code go here --%>
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

In Init.js file,

//Attach delegate functions to the init event to complete tasks that must
//be performed before objects are created. The add_init accessor binds a
//delegate function to the init event.
Sys.Application.add_init(AppInit);
function AppInit(sender) {  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(End);
}

//The endRequest event is raised after an asynchronous postback is finished
//and control has been returned to the browser. You can use this event to
//provide a notification to users or to log errors.
function End(sender, args) { }

No comments:

Post a Comment