jQuery 'Sys' 未定义 - Ajax - master 和 contentpages ASP.NET 4.0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15442548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Sys' is undefined - Ajax - master and contentpages ASP.NET 4.0
提问by J.Olsson
I've been having this problem for quite a while now and tried various solutions, Editing the Webconfig with various tags.
我遇到这个问题已经有一段时间了,并尝试了各种解决方案,用各种标签编辑 Webconfig。
Im using Updatepanels, timers and AJAX on my webpage.(can it be AJAX that throws the error?)
我在我的网页上使用 Updatepanels、计时器和 AJAX。(会不会是 AJAX 引发错误?)
I really dont know how to define the problem, becuase i really dont know where to trace it.
我真的不知道如何定义问题,因为我真的不知道从哪里追踪它。
I've placed a onload on the body in the masterpage (dont know if thats right) Heres some markup code and my Javascript code that i think throws the error.
我在母版页的正文上放置了一个加载项(不知道是否正确) 这里有一些标记代码和我认为会引发错误的 Javascript 代码。
MASTERPAGE:
母版:
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<meta charset="utf-8" />
<title><%: Page.Title %> - </title>
<link href="~/Content/Site.css" rel="stylesheet" />
<link href="~/Content/BarChart.css" rel="stylesheet" />
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
<asp:PlaceHolder runat="server">
<script src="<%: ResolveUrl("~/Scripts/_references.js") %>"></script>
<script src="<%: ResolveUrl("~/Scripts/modernizr-2.5.3.js") %>"></script>
<script src="<%: ResolveUrl("~/Scripts/jquery-1.7.1.js") %>"></script>
<script src="<%: ResolveUrl("~/Scripts/jquery-1.7.1.min.js") %>"></script>
<script src="<%: ResolveUrl("~/Scripts/jquery-ui-1.8.20.js") %>"></script>
<script src="<%: ResolveUrl("~/Scripts/jquery-ui-1.8.20.min.js") %>"></script>
<script src="<%: ResolveUrl("~/Scripts/ButtonManager.js") %>"></script>
<script src="<%: ResolveUrl("~/Scripts/BarChart.js") %>"></script>
</asp:PlaceHolder>
<meta name="viewport" content="width=device-width" />
<asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>
<body >
<form runat="server">
<asp:ScriptManager runat="server">
<Scripts>
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="jquery.ui.combined" />
</Scripts>
</asp:ScriptManager>
<header>
<div class="content-wrapper">
<div class="float-left">
<asp:ImageButton ID="Logo" cssclass="site-title" runat="server" PostBackUrl="~/" ImageUrl="~/Images/Logo.png" />
</div>
<div class="float-right">
<section id="login">
<asp:LoginView runat="server" ViewStateMode="Disabled">
<AnonymousTemplate>
<ul>
<li><a id="registerLink" runat="server" href="~/Account/Register.aspx">Register</a></li>
<li><a id="loginLink" runat="server" href="~/Account/Login.aspx">Log in</a></li>
</ul>
</AnonymousTemplate>
<LoggedInTemplate>
<p>
Hello, <a runat="server" class="username" href="~/Account/Manage.aspx" title="Manage your account">
<asp:LoginName runat="server" CssClass="username" />
</a>!
<asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" />
</p>
</LoggedInTemplate>
</asp:LoginView>
</section>
<nav>
<ul id="menu">
<li><a runat="server" href="~/">Home</a></li>
<li><a runat="server" href="~/Barchart.aspx">About</a></li>
<li><a runat="server" href="~/Contact.aspx">Contact</a></li>
</ul>
</nav>
</div>
</div>
</header>
<div id="body">
<asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
<section class="content-wrapper main-content clear-fix">
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</section>
</div>
<footer>
<div class="content-wrapper">
<div class="float-left">
<p>© <%: DateTime.Now.Year %> - My ASP.NET Application</p>
</div>
</div>
</footer>
</form>
</body>
</html>
CONTENTPAGE:
内容页:
<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
</asp:Content>
<asp:Content runat="server" ID="BodyContentHome" ContentPlaceHolderID="MainContent" >
<asp:Panel ID="UpdatepanelWrapper" CssClass="Updatepanelwrapper" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:PlaceHolder runat="server" ID="WholeWrapper">
<asp:PlaceHolder runat="server" ID="QuestionWrapper">
<asp:PlaceHolder runat="server" ID="LabelQuestion"></asp:PlaceHolder>
<asp:PlaceHolder runat="server" ID="Question"></asp:PlaceHolder>
</asp:PlaceHolder>
</asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnDelete" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:Panel ID="ButtonPanel" CssClass="ButtonPanel" runat="server">
<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/Images/Deleteicon.png" CssClass="DeleteButtonHidden" OnClientClick="btnDelete_Click" />
<asp:ImageButton ID="btnAdd" runat="server" ImageUrl="~/Images/Addicon.png" CssClass="AddButtonMoreMargin" OnClientClick="btnAddQuestion_Click" />
</asp:Panel>
</asp:Panel>
<asp:Button ID="btnSendQuestions" runat="server" Text="Skapa F?rel?sning och fr?gor" OnClick="btnSendQuestions_Click" ValidationGroup="QuestionGroup" />
</asp:Content>
JAVASCRIPT CODE:
爪哇代码:
if (window.addEventListener) window.addEventListener("load", load(), true);
else window.onload = load();
function load() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}
function EndRequestHandler(sender, args) {
// NOT IMPORTANT
}
回答by J.Olsson
I just solved the problem. I think. I knew the function was run before the DOM was build.
我刚刚解决了这个问题。我认为。我知道该函数是在构建 DOM 之前运行的。
So i just did this
所以我只是做了这个
$(document).ready(function () {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
});
One problem yet presists. When i clicking on the Imagebutton btnAdd The console says:
一个问题仍然存在。当我点击 Imagebutton btnAdd 控制台说:
Uncaught ReferenceError: btnAddQuestion_Click is not defined
onclick
this directs me to my code line where this ImageButton is written.
这会将我定向到编写此 ImageButton 的代码行。
Internet Explorer doesn't like this at all. but other browsers just proceed like it was nothing, even if the error message appears in the Dev-Console.
Internet Explorer 根本不喜欢这个。但是其他浏览器就像什么都没有一样继续,即使错误消息出现在 Dev-Console 中。
The Button functionality works to. the only problem is the error.
按钮功能适用于。唯一的问题是错误。
EDIT: This is now Solved. i was using "OnClientClick". on the ImageButtons. I changed it to OnClick and that solved this issue. I dont know the difference between theese two, but im happy as long as it works.
编辑:现在解决了。我正在使用“OnClientClick”。在图像按钮上。我将其更改为 OnClick 并解决了这个问题。我不知道这两者之间的区别,但只要它有效,我就很高兴。
回答by Hawkeye
All you should need to do is set EnableCdn="true"
in your script manager,
like this:
您需要做的就是EnableCdn="true"
在脚本管理器中进行设置,如下所示:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableCdn="true" />
Basically, the Sys
library isn't downloaded for one reason or another, and if you set EnableCdn="true"
it will use Microsoft's Content Delivery Network (CDN) to download the scripts that you need, including the Sys
library.
基本上,Sys
不会出于某种原因下载库,如果您设置EnableCdn="true"
它,它将使用 Microsoft 的内容交付网络 (CDN) 下载您需要的脚本,包括Sys
库。
I have a more detailed explanation in thispost.
我在这篇文章中有更详细的解释。
回答by M Kelly
In my case I found that the culprit was caused by missing the call to MyBase.OnPreRenderComplete(e) in the overridden version of the function/method OnPreRenderComplete.
在我的例子中,我发现罪魁祸首是在函数/方法 OnPreRenderComplete 的重写版本中错过了对 MyBase.OnPreRenderComplete(e) 的调用。
By including the call to MyBase.OnPreRenderComplete(e) everything works nicely. If the call to MyBase.OnPreRenderComplete(e) is missing then the error "Uncaught ReferenceError: Sys is not defined" is received and many things don't work properly.
通过包括对 MyBase.OnPreRenderComplete(e) 的调用,一切都很好。如果缺少对 MyBase.OnPreRenderComplete(e) 的调用,则会收到错误“Uncaught ReferenceError: Sys is not defined”并且许多事情无法正常工作。
Protected Overrides Sub OnPreRenderComplete(ByVal e As EventArgs) 'MyBase.OnPreRenderComplete(e) ... End Sub
Protected Overrides Sub OnPreRenderComplete(ByVal e As EventArgs) 'MyBase.OnPreRenderComplete(e) ... End Sub
回答by Amol Khandagale
For asp.net page
对于asp.net页面
Just add Scriptmanager on page.
只需在页面上添加 Scriptmanager。
How to add Go to Toolbar (left side of Visual studio)--> AJAX Extensions --> Scriptmanager --> Double click or drag and drop
如何添加转到工具栏(Visual Studio 左侧)--> AJAX 扩展--> Scriptmanager --> 双击或拖放
:)
:)
回答by Fernando Meneses Gomes
In my case the problem was that I had putted the following code to keep the gridview tableheader after partial postback:
在我的情况下,问题是我已经放置了以下代码以在部分回发后保留 gridview 表头:
protected override void OnPreRenderComplete(EventArgs e)
{
if (grv.Rows.Count > 0)
{
grv.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
Removing this code stopped the issue.
删除此代码停止了问题。
回答by Santiago quits SO
Another solution is to place the script tag before the html close tag at the bottom of your webform or master page:
另一种解决方案是将 script 标签放在 webform 或母版页底部的 html close 标签之前:
<html>
<body>
.
.
.
</body>
<script type="text/javascript" src="/scripts/script.js"></script>
</html>
In case of a webform using a master page you'll have to place the script tag before the closing </asp:Content>
tag.
如果使用母版页的网络表单,您必须将脚本标记放在结束</asp:Content>
标记之前。