从asp.net代码调用Javascript函数 [英] Calling Javascript function from asp.net code behind

查看:57
本文介绍了从asp.net代码调用Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的asp.net代码页面后面调用javascript函数

我写了以下代码但是它不工作



I want to call javascript function from my asp.net code behind page
I have written following code but it is not working

System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(@"<script language='text\javascript'>");
        sb.Append(@"BlitzMap.setMapFromKML(document.getElementById('kmlString').value);");
        sb.Append(@"</script>");
        System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "JCall1", sb.ToString(), false);





任何人都可以告诉我这个代码有什么问题。



can anyone tell me whats wrong with this code.

推荐答案

Javascript在浏览器中执行,服务器上的代码隐藏 - 所以实际调用来自代码隐藏的javascript代码会涉及很多诡计。



我怀疑这不是你想要做的事情,你只是想要执行某些事情一旦文件满载。



如果是这种情况你可以添加这样的东西:

Javascript executes in the browser, the codebehind on the server - so actually calling the javascript code from the codebehind would involve a lot of trickery.

I suspect that this isn''t really what you attempt to do, you just want something to execute once the document is fully loaded.

If that is the case you could add something like this:
window.onload = function ()
 {
  // Your code goes here...
 };



你的javascript代码。



祝你好运

Espen Harlinn


to your javascript code.

Best regards
Espen Harlinn


在页面的head部分写下你的javascript函数你通常这样做,





Write your javascript function in head section of your page as you normally do,


<head>

<script type="text/javascript">
yourfunction()

{}
</script>

</head>





然后从代码隐藏中调用它,如下所示:





and then call it from codebehind like this:

Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", yourfunction(), false);


这篇关于从asp.net代码调用Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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