Updated your web.config, but Sys is still undefined?
AJAX, ASP.NET, JavaScript By Dave Ward on August 16th, 2007
If you Google the error you’ll find a lot of results about updating your web.config to enable ASP.NET AJAX (which is a necessary step), but what if you’ve already done that? Not only will your search be an exercise in frustration, but it will lead you down the wrong path completely. Maybe I can help.
What if Sys really is undefined?
After web.config problems, the most common reason for this error is JavaScript that references the Sys namespace too early. Take this code, for example:
<head>
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(End);
function End(sender, args) { }
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm1" runat="server" />
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<!-- Interesting content goes here -->
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>Loading this page will result in a Sys undefined error, identical to the one in the screenshot above. The reason for it is simple: The Sys namespace is undefined when the script block executes.
The ScriptManager control injects JavaScript includes to initialize the ASP.NET AJAX Framework, the ScriptManager, and then the UpdatePanel, but it injects the bulk of that JavaScript in the exact location that the ScriptManager control is positioned at in the page. So, the script block in the page head tries to access the PageRequestManager long before the AJAX Framework has been fully loaded. Hence the error.
The same thing happens if you try to include the same JavaScript code from an external file in the head, or even in the ScriptManager’s Scripts collection.
The Quick Fix
The most obvious fix for this is to move the script block below the ScriptManager control:
<asp:ScriptManager ID="sm1" runat="server" />
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(End);
function End(sender, args) { }
</script>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<!-- Interesting content goes here -->
</ContentTemplate>
</asp:UpdatePanel>That’s the same JavaScript code, but now it works because it’s referencing the PageRequestManager after the AJAX Framework has been initialized. While it does work, hopefully this solution doesn’t sit well with you. It’s not a very good one.
A Better Way
The correct way to remedy 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:
<asp:ScriptManager ID="sm1" runat="server">
<Scripts>
<asp:ScriptReference Path="Init.js" />
</Scripts>
</asp:ScriptManager>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<!-- Interesting content goes here -->
</ContentTemplate>
</asp:UpdatePanel>Init.js:
Sys.Application.add_init(AppInit); function AppInit(sender) { Sys.WebForms.PageRequestManager.getInstance().add_endRequest(End); } function End(sender, args) { }
This is much better. Not only is it a bit more sturdy, but it’s just good practice to separate presentation from functionality as much as possible.
Remember, all of your JavaScript functions are globally scoped. So, the AppInit function could still call functions defined at the page level or in other script includes. This is especially handy if you want to use Control.ClientID to more robustly reference controls in your client script, since you can’t do that inside a JavaScript include file.
Similar posts
What do you think? Your comments are welcome.
I appreciate all of your comments, questions, and other feedback, but please try to stay on topic. If you have a question unrelated to this post, I recommend posting on the ASP.NET forums instead.
If you're replying to an existing comment, please use the threading feature. To do this, click the "Reply to this" link underneath the comment you're replying to.
2 Mentions Elsewhere
- ASP.NET disable a button control during postback « Basketman's Blog
- asp.net ajax 1.0 中出现 ‘Sys’ is undefined 错误 | Nothing but coding


hi,
why is it that the script manager was removed from being added inside the head tag(atlas days)? we might not be able to have that issue?
great blog
It’s output was probably moved to improve the perceived page load performance. Because scripts in the head block the browser from rendering until after they’ve run, delaying them a bit makes the page load feel quicker and more responsive.
Dear Dave
I am using following code, and aslo created js file with same name (Init.js).
But it does not work for me. Do I need to do some thing else…..because I could not understand the line… “then call our wireups in a Sys.Application.init event handler “.
What it means…. ?
thanks
goood 5 star from me
Good on you. Nice article – thanks for the heads up and the use of the Scripts collection – otherwise I would have probably done the ‘quick & dirty’ way.
cheers,
Dan
Great article, thanks.
I really love Dave’s articles cause they help me to solve many basic problems of Ajax and they are excellent tips and tricks. It is hard to find solutions somewhere else.
johnaspnet
Thanks pal,
That made a lot of sense and its working for me !!! great ….
Hi
My code works fine in my local machine but i get
“Sys is undefined” error on testing server.
Can you please help me how to solve this problem
regards
gprashantraj
The testing server probably doesn’t have the AJAX Extensions installed on it. The need to be installed on the server, not just on your development machine.
hi
Thanks for your reply
AJAX Entension is already installed on the server.
I get this error when i press enter key in password textbox. I have placed the code in separate JS file and added ScriptReference to Script Manager and implemented the above concept as it is.
Post the ASPX and C#/VB code for that page on the asp.net forums, and email me a link. I’ll try to take a look at it for you.
You are a genius. I was trying to play around with some asp.net ajax code and I kept getting this error and most of my search as you said was taking me down the wrong path.
Dude, you saved my bloody day! I’d hug you if I could!
Hi I Do as you write but Still have same error sys.not defined
If so, then it’s probably your web.config. Specifically, double check the HttpHandlers and HttpModules. It sounds like you may have got the assembly references added, but not the ScriptResource handler.
Perfect, just what I needed!
I have tried these steps but still get the dreaded ‘Sys’ is undefined. The problem I am having is only when I place the ASP.NET 2.0 AJAX page on a server in a web farm. If I diplay the page on one of the servers using http://localhost/TestPage.aspx it displays fine. If I use http://www.hostname.com/TestPage.aspx I get the ‘Sys’ is undefined error and clicking AJAX controls causes a post back of the whole page. If I drop the same page on a server that is not on a web farm then I have no problems. Any advice much appreciated.
I am using VS 2008 and I am getting Sys is Undefined error on some pages which rest work fine, I tried whats suggested and it doesnt work for me.
Please suggest what to do next.
Cheers
you can simply set the LoadScriptsBeforeUI property to true and this will solve your problem
I am using Asp.net Ajax 1.0 and want to do 301 redirect from global.asax Application_BeginRequest method. But it is giving a javascript error as “sys is not defined”.
I am using global.asax to redirect as I have to create dynamic Urls from database. When the remove the code from Application_BeginRequest block every thing is working fine.
Please help me out with some solution.
If I had to guess, I’d guess that your URL rewriting is interfering with the ScriptResource.axd handler that ASP.NET AJAX uses to host its JavaScript libraries. Try excluding *.axd from your global.asax code.
I have tried everything and have gone through 20 different articles and still can’t get rid of my Sys not defined error. Already have checked everything in my config file. Any suggestions?
Thanks in advance!
I guess the obvious question is, did you check for issues similar to what I outlined in this post?
I’ve been wrestling with this on and off for months. (After a few hours of it, I just go back to 2.0 and forget the AJAX, only to return again.)
I have Framework 3.5 installed, which if I understand it means I already have AJAX. Tried this on two different servers, IE6 and IE7. I use a text editor to create ASP, not Visual Studio, but I copied a VS-created web.config, copied others from 4GuysFromRolla, etc., and have tried all the web.config steps recommended in many other posts.
I’ve tried your solution, too, but still get the ‘Sys’ is undefined. It’s happening near the very end of the page source, on Sys.Application.initialize();, even though other lines above refer to the Sys object, such as:
//
and
//
Do you guys have any ideas of avenues I can pursue? Thanks.
Ooops, I tried to post with example script code, but forgot the pre tag and was probably flagged as spam or something.
Anyway, I’ve been having this ‘Sys’ is undefined problem for months. Every time I wrestle with it for a few hours, read all the blogs and forums, modify web.config, change browsers, etc., it’s still there and I finally give up, only to come back and try again.
I’ve tried the solution above, and it still doesn’t work.
I have the Framework 3.5 installed, so I assume that means I already have AJAX capability and don’t need any special files in my bin folder, etc. I use a text editor to write code, not Visual Studio, so perhaps that’s related to my problem. I’ve run Visual Web Developer 2008 Express to create a web.config, and have tried multiple others I copied from the web.
My error comes near the end of the page, in View Source on a line that says
Sys.Application.initialize();
Higher up in the page, there are other references to the Sys object, but these don’t appear to be creating errors. For example, near the top of the page,
and a little further down,
Could anyone suggest an avenue I could pursue which might lead to a solution?
Thanks.
The first thing I’d check is that the ScriptResource.axd and WebResource.axd calls are still working properly when you move to 3.5.
guy I have the same problem and the solution dont work, I use vista ultimate and visual studio 2008. I dont know what happened?
Can you give me some tip to debug its, so I can find a solution.
i had the same issue & tried to slove it using this solution.
now i’dont get sys is undefined error
but some time i have seen en error saying
“Sys.WebForms.PageRequestManager is undefined”
i’am using Ajax Tab panel on the master page
Could anyone suggest a solution to this issue?
Thanks
Make sure any of your JavaScript code that references the PageRequestManager (like code that sets up BeginRequest and EndRequest handlers) isn’t executing until Application.Init.
Thank you ..ur blog was of grt help :)
To all you people who are still having problems…. make sure your referenced .dll’s are all dated (created) after the AjaxControlToolkit.dll’s creation date
Hi. I was trying the solution that you post. But I still have the same error. One thing that I noticied was that in the JScript file (.js) the intellisense doesnt show the Sys Class. The Warning that I am reciving is:
“Error Updating JScript IntelliSense: (File Path).js: ‘sys’ is not defined @ (Row, File)”
Any clue about this Error
Thanks for all
Hi Dave,
Nice article. I too get this error (in production….dead duck i am). But it is inconsistent. I have all modules, handlers added properly.
Is that like scriptmanager does its task async.. I mean may be sys object is not at all created and then it’s referring to sys via any AJAX control.
I missed out that I am using Telerik controls.
Pls Help!!
Help, I have the same problem and I also use Telerik. Have you solved the problem? How did you solved it?
Thanks a lot for your help
Hmmm … I followed this to a T, but still get the same problem — Sys.WebForms is undefined. Sys.Application is fine, but not Sys.WebForms.
What else could be wrong?
Thx
Josh
This problem turns out to have its roots in a legacy page (where legacy means “first built with .NET 1.1″). When the markup and code are copied into a freshly built ASP.NET Ajax web form, the right thing happens.
It’s not clear to me exactly what the problem is, although one possible culprit is that new pages have while old pages have just a element.
Cheers
Josh
Check your web.config for <xhtmlConformance mode=”Legacy”/>. It is automatically added during the 1.x upgrade process and breaks ASP.NET AJAX: http://weblogs.asp.net/scottgu/archive/2006/12/10/gotcha-don-t-use-xhtmlconformance-mode-legacy-with-asp-net-ajax.aspx
Keep up the great work!!
thanks a lot
try using an ajax enabled website instead
it worked for me
Thanks a ton
I am looking for same solution. I am getting ‘sys is not defined’ error
any solution?
Thanks, the error is resolved.