如何从 .cs 文件 C# 调用 JavaScript/jQuery 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6762441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call JavaScript/jQuery function from .cs file C#
提问by Irfan
I'm working on asp.net (C#) project which include some page files (aspx, aspx.cs) and some only (.cs) file. I'm able to access JavaScript/jQuery functions from page files (aspx, aspx.cs) using scriptregister
. but my question is how can I access the JavaScript/jQuery functions from (.cs) files. I'm generating some html tags in (.cs) file therefore in need of to call JavaScript/jQuery function from (.cs) classes.
我正在研究 asp.net (C#) 项目,其中包括一些页面文件(aspx、aspx.cs)和一些仅(.cs)文件。我能够从页面文件(aspx、aspx.cs)使用scriptregister
. 但我的问题是如何从 (.cs) 文件访问 JavaScript/jQuery 函数。我正在 (.cs) 文件中生成一些 html 标签,因此需要从 (.cs) 类调用 JavaScript/jQuery 函数。
The files I have are(for example):
我拥有的文件是(例如):
- Default.aspx
- Default.aspx.cs
- And Web.cs (My concern is to call JavaScript/jQuery function from web.cs)
- 默认.aspx
- 默认.aspx.cs
- 和 Web.cs(我关心的是从 web.cs 调用 JavaScript/jQuery 函数)
Actually, What I'm trying to do is to generate some HTML code in Web.cs (Business Object) and while generating HTML code in web.cs i need some other dynamic HTML code generated by jQuery function which output is my concern: var output ="My HTML Code Generated by jQuery function"
实际上,我想要做的是在 Web.cs(业务对象)中生成一些 HTML 代码,而在 web.cs 中生成 HTML 代码时,我需要一些由 jQuery 函数生成的其他动态 HTML 代码,这些代码的输出是我所关心的: var output ="My HTML Code Generated by jQuery function"
Now I need the above jQuery output to merger with Web.cs HTML code generating.
现在我需要将上面的 jQuery 输出与 Web.cs HTML 代码生成合并。
Kindly let me know how can it be possible to call jQuery function from web.cs and get back the result from jQuery to web.cs in order to merger?
请让我知道如何从 web.cs 调用 jQuery 函数并将结果从 jQuery 返回到 web.cs 以便合并?
Thanks
谢谢
回答by asawyer
This question doesn't make alot of sense honestly. You may have a misconception about how this all works together.
老实说,这个问题没有多大意义。您可能对这一切如何协同工作有误解。
The c# code is running on the web server, and the output is Html code that is rendered down to the browser, including javascript.
c# 代码在 Web 服务器上运行,输出是向下呈现到浏览器的 Html 代码,包括 javascript。
The Javascript is loaded into the browser and executed locally on the client's computer.
Javascript 被加载到浏览器中并在客户端的计算机上本地执行。
If your trying to manipulate or control some aspect of the client page load with javascript or something either add the script to the page itself (.aspx file) register and stream it with Client.Registerapi calls, or add a < script .... > tag to import it into the file.
如果您尝试使用 javascript 或其他方式操作或控制客户端页面加载的某些方面,请将脚本添加到页面本身(.aspx 文件)中,然后使用 Client.Registerapi 调用注册并流式传输它,或者添加 < 脚本 ... . > 标签将其导入到文件中。
I have no idea what Web.cs class is doing, I would assume its some kind of logic or business object. You'll need to communicate the need for the client script from it back to the presentation layer to get it to run client side.
我不知道 Web.cs 类在做什么,我会假设它是某种逻辑或业务对象。您需要将客户端脚本的需求从它传递回表示层,以使其在客户端运行。
If you are somehow under the impression that you can run javascript from the server side, you are mistaken.
如果您以某种方式认为您可以从服务器端运行 javascript,那么您就错了。
回答by Yochai Timmer
The ASP code is server-side.
The javascript is on the client side.
ASP 代码是服务器端的。
javascript 在客户端。
You can't really call the javascript.
You could create an html file via the ASP code that will generate an onload javascript method.
Then when the html is loaded, the javascript will run.
你不能真正调用 javascript。
您可以通过 ASP 代码创建一个 html 文件,该文件将生成一个 onload javascript 方法。
然后当加载 html 时,javascript 将运行。
回答by Ali Habibzadeh
This is done using Page.RegisterStartupScript Method
这是使用Page.RegisterStartupScript 方法完成的
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public void Page_Load(Object sender, EventArgs e)
{
String csname1 = "PopupScript";
String csname2 = "ButtonClickScript";
if (!IsClientScriptBlockRegistered(csname1))
{
String cstext1 = "<script type=\"text/javascript\">" +
"alert('Hello World');</" + "script>";
RegisterStartupScript(csname1, cstext1);
}
if (!IsClientScriptBlockRegistered(csname2))
{
StringBuilder cstext2 = new StringBuilder();
cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
cstext2.Append("Form1.Message.value='Text from client script.'} </");
cstext2.Append("script>");
RegisterClientScriptBlock(csname2, cstext2.ToString());
}
}
</script>
<html >
<head>
<title>RegisterClientScriptBlock Example</title>
</head>
<body>
<form id="Form1"
runat="server">
<input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
</form>
</body>
</html>
回答by jvenema
Unless you have an absolutely rock solid use case, you should reconsider having your JavaScript rendering from c# at all.
除非你有一个绝对可靠的用例,否则你应该重新考虑从 C# 渲染你的 JavaScript。
Typically, it's much simpler to separate the two. Generate your markup using c#, and "tag" the appropriate elements (using CSS classes probably, since ASP.NET has such awful handling of ids).
通常,将两者分开要简单得多。使用 c# 生成标记,并“标记”适当的元素(可能使用 CSS 类,因为 ASP.NET 对 id 的处理如此糟糕)。
Then simply use jQuery to find and manipulate those tagged elements, but do so using an external js script, rather than generating the JavaScript itself inline.
然后只需使用 jQuery 来查找和操作这些标记元素,但使用外部 js 脚本执行此操作,而不是内联生成 JavaScript 本身。
回答by ar3
You could pass in the ScriptManager to Web.cs and then use the same methods you use in the code-behind, but you might be better off just constructing and returning the javascript string from Web.cs, and still calling js from your code-behind like you have been doing.
您可以将 ScriptManager 传递给 Web.cs,然后使用您在代码隐藏中使用的相同方法,但您最好只从 Web.cs 构建和返回 javascript 字符串,并且仍然从您的代码中调用 js-后面就像你一直在做的。
回答by Andrew Peacock
If you simply want to register some client script from a stand alone cs file, the only way I can see this working is to pass your function a reference to the ClientScriptManager for the page.
如果您只是想从独立的 cs 文件中注册一些客户端脚本,我能看到此工作的唯一方法是将您的函数传递给页面的 ClientScriptManager 引用。
回答by andzt
The script registration actually happens on the "Page" object, i.e. Web.cs does not have access to the page to register a script. For example, if you want Web.cs to register a script on Default.aspx page, you'd need to give access to the Default.aspx/cs "Page" object's Page.ClientScript.RegisterClientScriptBlock method through an interface or class definition.
脚本注册实际上发生在“页面”对象上,即 Web.cs 无权访问页面来注册脚本。例如,如果您希望 Web.cs 在 Default.aspx 页上注册脚本,则需要通过接口或类定义授予对 Default.aspx/cs“Page”对象的 Page.ClientScript.RegisterClientScriptBlock 方法的访问权限。
回答by Peter
JavaScript is client side and c# is server side so you can't call JavaScript from server side, but you can schedule a JavaScript to be executed when the page is rendered on the client side...
JavaScript 是客户端,c# 是服务器端,因此您不能从服务器端调用 JavaScript,但是您可以安排在客户端呈现页面时执行 JavaScript...