如何从代码后面调用javascript函数? [英] How do i call javascript function from code behind?

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

问题描述

我有一个Javascript文件,我已经从互联网上获取了我的项目。

我创建了一个Javascript项目,并在那里粘贴了Javascript代码,并将其命名为Javascript.js。



我已经在设计页面中使用



I have a Javascript file that I have gotten off the internet that I need for my project.
I created a Javascript item and pasted the Javascript code there and named it Javascript.js.

I've imported it in the design page using

<script type="text/javascript" src="JavaScript.js"></script>







现在,在我的代码隐藏文件中,我需要称之为有趣ction






Now, in my code behind file, I need to call this function

var ciphertext = Salsa20.encrypt(key, message, nonce, counter, options);





如何实现这一目标?



我尝试过的事情:



如果我在.aspx文件中编写了脚本,那么在线的所有教程都会提到我需要做什么。但由于我有一个单独的.js文件,我不知道如何实现这个



How do I achieve this?

What I have tried:

All the tutorials online mention what I need to do if I have written the script in the .aspx file. But since I have a separate .js file, I am not sure how to implement this

推荐答案

有几种方法可以调用它

1.使用scriptManager

2.使用Page.RegisterStartupScript方法(不含scriptManager)



withscript manager

There are couple of ways to call it
1. using scriptManager
2. using Page.RegisterStartupScript method (without scriptManager)

withscript manager
ScriptManager.RegisterStartupScript(this, this.GetType(), "methodName","methodName('" + Name + "','Keyword',+'Region');", true);





没有脚本管理器



without script manager

Page.RegisterStartupScript("methodName", "methodName('" + Name + "','Keyword',+'Region');");





静态JavaScript文件通常不会通过ASP.NET提供,我认为你需要制作页面上的函数,然后从后面的代码调用它



Static JavaScript files do not get fed through ASP.NET normally, I think you need to make the function on page and then call it from code behind


你不能从你的代码隐藏调用javascript,因为javascript是浏览器理解和执行的东西。不要听那些谈论RegisterStartupScript等的人,他们只会让你误解错误信息。您的c#代码在.net环境中的服务器上运行,并且不理解javascript。



你必须将js函数的内容翻译成c#代码你可以使用(如果用于加密事物则很可能存在等效的.net函数),或者进行多阶段处理,生成一个页面,其中只有一个表单可以完成工作并将值放入隐藏字段然后自动提交表单,你的页面可以读取那个值。
You can't call javascript from your code-behind because javascript is something understood by and executed by the browser. Don't listen to people who talk about RegisterStartupScript etc, they're simply confusing you with misinformation. Your c# code runs on the server in the .net environment and does not understand javascript.

You'll have to either translate what the js function does into c# code you can use (if it is for encrypting things then chances are there are equivalent .net functions), or do multiple-stage processing where you generate a page that simply has a form that does the work and puts the value in a hidden field then automatically submits the form, and your page can read the value that way.


if(!Page.IsPostBack)

{



if(!Page.IsClientScriptBlockRegistered(MyScript))

{

Page.RegisterClientScriptBlock(MyScript,< SCRIPT Language = 'JavaScript'src ='js / OPD / patients / jsfileName.js'>< / SCRIPT>);

}



dName.Attributes.Add(onClick,fnNa me(););

}
if (!Page.IsPostBack)
{

if (! Page.IsClientScriptBlockRegistered("MyScript"))
{
Page.RegisterClientScriptBlock("MyScript", "<SCRIPT Language='JavaScript' src='js/OPD/patients/jsfileName.js'></SCRIPT>");
}

dName.Attributes.Add("onClick", "fnName();");
}


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

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