为什么我需要的RegisterStartupScript在asp.net一个简单的jQuery的HelloWorld? [英] Why do I need RegisterStartupScript for a simple jquery helloworld in asp.net?
问题描述
我试图只是里面包括表单JavaScript,但运行时页面上都Chrome和Firefox的空白打电话给我的HelloWorld。在Firefox误差
XML解析错误:未找到元素
块引用><%@页面语言=C#AutoEventWireup =真codeBehind =Default.aspx.cs继承=jquery01._Default%GT;!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>< /标题>
<脚本类型=文/ JavaScript的SRC =http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js>
< / SCRIPT>
<脚本类型=文/ JavaScript的>
功能的helloWorld(){
$(#divSample)追加(的Hello World!);
}
< / SCRIPT>
< /头>
<身体GT;
<表ID =form1的=服务器>
< DIV ID =divSample> < / DIV>
<脚本类型=文/ JavaScript的>的helloWorld();&下; /脚本>
< /表及GT;
< /身体GT;
< / HTML>我需要在codebehind添加:
保护覆盖无效渲染(HtmlTextWriter的作家)
{ this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(),
启动,<脚本类型= \\文/ JavaScript的\\>的helloWorld();< / SCRIPT>中);
base.Render(作家);
}在这种情况下,它的工作原理,但我不明白,为什么我不能用第一个语法,为什么它是这样一个简单的东西吗?
那么复杂我也试过了建议,但它没有工作之一:
<%@页面语言=C#AutoEventWireup =真codeBehind =Default.aspx.cs继承=jquery01._Default%GT;!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>< /标题>
<脚本类型=文/ JavaScript的SRC =http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js>
< / SCRIPT>
<脚本类型=文/ JavaScript的>
功能的helloWorld(){
$(#divSample)追加(的Hello World!);
}
< / SCRIPT>
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
你好,世界();
});
< / SCRIPT>< /头>
<身体GT;
<表ID =form1的=服务器>
< DIV ID =divSample> < / DIV>
< /表及GT;
< /身体GT;
< / HTML>更新:好像ASP.NET可以在某些情况下,使用Ajax / jQuery的不可靠?
的http://chiragrdarji.word$p$pss.com/2010/02/17/xml-parsing-error-no-element-found/解决方案请尝试使用你的脚本标签的
延迟=延迟
属性。<脚本类型=文/ JavaScript的延迟=延迟>的helloWorld();< / SCRIPT>
如果您尝试修改DOM结构的HTML仍然被呈现当IE不喜欢。这个属性告诉浏览器,直到它与渲染完成的延迟脚本执行。
I tried to call my helloworld by just including javascript inside webform but when running it page is blank on both chrome and firefox. In firefox error is
"XML Parsing Error: no element found"
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jquery01._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"> </script> <script type="text/javascript"> function helloWorld() { $("#divSample").append("Hello World!!"); } </script> </head> <body> <form id="form1" runat="server"> <div id="divSample"> </div> <script type="text/javascript"> helloWorld();</script> </form> </body> </html>
I needed to add in codebehind:
protected override void Render(HtmlTextWriter writer) { this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "startup", "<script type=\"text/javascript\">helloWorld();</script>"); base.Render(writer); }
In that case it works but I don't understand why I just can't use the 1st syntax why it's so complicated for such a simple stuff ?
I also tried the suggestion but it didn't work either:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jquery01._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"> </script> <script type="text/javascript"> function helloWorld() { $("#divSample").append("Hello World!!"); } </script> <script type="text/javascript"> $(document).ready(function () { helloWorld(); }); </script> </head> <body> <form id="form1" runat="server"> <div id="divSample"> </div> </form> </body> </html>
Update: Seems ASP.NET can be unreliable in some circumstances with ajax / jquery ? http://chiragrdarji.wordpress.com/2010/02/17/xml-parsing-error-no-element-found/
解决方案try using a
delay="delay"
attribute on your script tag.<script type="text/javascript" delay="delay">helloWorld();</script>
IE does not like if you try to modify the DOM structure when the html is still being rendered. this attribute tells the browser to delay script execution until it is done with the rendering.
这篇关于为什么我需要的RegisterStartupScript在asp.net一个简单的jQuery的HelloWorld?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!