如何在aspx.cs页面中调用javascript函数? [英] how to call javascript function in aspx.cs page?

查看:177
本文介绍了如何在aspx.cs页面中调用javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在aspx.cs页面中调用javascript函数?

解决方案

尽管本网站上发布的示例有很多答案,但您从未真正调用Javascript ASP.NET代码。如果您了解服务器端和客户端发生的情况,这将变得非常明显。你后面的ASP.NET代码在服务器端工作。它所做的就是处理HTTP请求并在HTTP响应中生成一些内容。当你问起Javascript时,这个内容是HTML,Javascript以及任何可以附带HTML的东西。



ASP.NET代码对Javascript有什么作用?如果生成它,可能有一些代码片段(通常是一些立即常量,从ASP.NET应用程序的角度来看是可变的)计算和生成动态。当带有Javascript的HTML页面到达客户端时,Web浏览器会根据您应该知道的某些规则调用Javascript函数,以响应浏览器中调用的事件。从这时起,这段代码完全与无关到ASP.NET,以及任何其他服务器端技术。因此,了解Javascript如何单独工作,无需与ASP.NET连接。



另请参阅: http://msdn.microsoft.com/en-us/library/System.Web.UI.ClientScriptManager%28v = vs.110%29.aspx [ ^ ]。



-SA


下面的例子展示了如何从.cs文件中调用javascript函数...





  string  script =  <脚本类型= \text / javascript \> javascriptFuntion();< / script>; 

// 如果不使用更新面板,你可以使用这个脚本...
// this.Page.ClientScript.RegisterStartupScript(typeof(Page),callfn,script );

// 如果您使用的是ajax更新面板,请使用以下代码行..
ScriptManager.RegisterStartupScript( typeof (页面), callfn,脚本, false );


参考从代码隐藏页面(Asp.Net)调用Javascript函数 [ ^ ]。



非常好解释。 :)

how to call javascript function in aspx.cs page?

解决方案

Despite of many "answers" with examples published on this site, you never really call Javascript from ASP.NET code. It will became quite obvious if you understand what happens on server side and on client side. You ASP.NET code behind works on the server side. All it does is processing the HTTP request and producing some content in HTTP response. As you ask about Javascript, this content is HTML, Javascript, and anything which can come with HTML.

What ASP.NET code does to Javascript? If generates it, perhaps having some fragments of code (usually some immediate constants, which are, from the standpoint of the ASP.NET application are variable) calculating and generating on the fly. And when the HTML page with Javascript arrives to the client side, a Web browser calls the Javascript functions, in response to events invokes in the browser, according certain rules you should know. From this moment, this code is totally agnostic to ASP.NET, as well as any other server-side technology. So, learn how Javascript works separately, without any connection to ASP.NET.

See also: http://msdn.microsoft.com/en-us/library/System.Web.UI.ClientScriptManager%28v=vs.110%29.aspx[^].

—SA


The below example shows how to call a javascript function from .cs file...


string script = " <script type=\"text/javascript\"> javascriptFuntion();   </script> ";

           // if not using update panel, u can use this script...
           //  this.Page.ClientScript.RegisterStartupScript(typeof(Page), "callfn", script);

           // if you are using ajax update panel use the below line of code..
           ScriptManager.RegisterStartupScript(this, typeof(Page), "callfn", script, false);


Refer Call Javascript function from codebehind page (Asp.Net)[^] by Sibasis.

Very nicely explained. :)


这篇关于如何在aspx.cs页面中调用javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆