简体   繁体   English

从C#和Asp.Net背后的代码调用JavaScript

[英]Calling javascript from codebehind c# & Asp.Net

Am unable to understand why Cannot the below javascript code is not called from code behind 我无法理解为什么不能从后面的代码调用下面的javascript代码

I have a simple javascript block like this 我有一个像这样的简单的JavaScript块

  function callsCox(res) {
    alert(res);
   }

From my code behind : 从我的代码后面:

 ....
 string res="COX23";
 string script = String.Format("callsCox({0})", res);
 this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Cox",script,true);

Am I missing anything? 我错过了什么吗? There aren't any exceptions or errors. 没有任何例外或错误。

Page.ClientScript.RegisterStartupScript looks OK to me (might have missed something). Page.ClientScript.RegisterStartupScript看起来Page.ClientScript.RegisterStartupScript (可能遗漏了一些东西)。 Things to try 要尝试的事情

  1. Add apostrophes to the call - it's coming through as an object. 将撇号添加到调用中 - 它作为对象传递。 Try as a string 尝试作为一个字符串

    string script = String.Format("callsCox('{0}')", res); string script = String.Format(“callsCox('{0}')”,res);

  2. Is the string script Page.ClientScript.RegisterStartupScript being called after an update panel partial postback. 是否在更新面板部分回发后调用字符串脚本Page.ClientScript.RegisterStartupScript That could effect it 那可能会影响它

  3. I have know functions not been found if they are in the same page. 我知道如果它们在同一页面中,则找不到功能。 Try moving to an external js file. 尝试移动到外部js文件。 Don't asked me why this has resolved issues but it has a couple of times in the past for me. 不要问我为什么这已经解决了问题,但过去我曾经有过几次。

  4. Just for debug purposes take the function out of the equation all together, Try to get the alert working like this. 只是出于调试目的,将函数一起取出等式,尝试使警报像这样工作。 It will at least isolate the problem if it does work 如果它确实有效,它至少会将问题隔离开来

    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Cox","alert('Does this work?')",true); this.Page.ClientScript.RegisterStartupScript(this.GetType(),“Cox”,“alert('这有用吗?')”,true);

  5. View the source of the page. 查看页面的来源。 Is the function even written into the page (or alert from point 4). 该功能是否写入页面(或从第4点开始提醒)。 It should be. 它应该是。 If you put a breakpoint on the this.Page.ClientScript.RegisterStartupScript method is it being hit? 如果你在this.Page.ClientScript.RegisterStartupScript方法上设置一个断点,它是否被击中? Seems like it might not be. 好像它可能不是。

Apologies for not giving you a 'hey this is the solution' type of answer. 很抱歉没有给您“嘿,这是解决方案”类型的答案。 I've had stuff like this in the past and I've found it a matter of stripping things down until the problem has been isolated. 我过去曾经有这样的东西,我发现这个问题一直存在,直到问题被隔离开来。 Someone else may be able to spot an immediate problem of course. 其他人当然可以发现一个直接的问题。 Good luck. 祝好运。

This works for me: 这对我有用:

public static void ShowAlert(Page page, String message)
{
    String Output;
    Output = String.Format("alert('{0}');",message);
    page.ClientScript.RegisterStartupScript(page.GetType(), "Key", Output, true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM