如何从 .cs 文件 (C#) 调用 JavaScript/jQuery 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6775606/
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 to access the JavaScript/jQuery function from alone (.cs) file. I'm generating some html tags in (.cs) file therefore in need of to call JavaScript/jQuery functions from only (.cs) class.
我正在研究 asp.net (C#) 项目,其中包括一些页面文件(aspx、aspx.cs)和一些仅(.cs)文件。我可以使用 访问来自页面文件(aspx、aspx.cs)scriptregister
的 JavaScript/jQuery 函数,但我的问题是如何从单独的 (.cs) 文件访问 JavaScript/jQuery 函数。我正在 (.cs) 文件中生成一些 html 标签,因此需要仅从 (.cs) 类调用 JavaScript/jQuery 函数。
The file I have are, for example, Default.aspx
and Default.aspx.cs
and Web.cs
(My concern is to call JavaScript/jQuery functions from web.cs)
该文件我有,例如,Default.aspx
与Default.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 functions from web.cs and get back the result from jQuery to web.cs in order to merge?
请让我知道如何从 web.cs 调用 jQuery 函数并将结果从 jQuery 返回到 web.cs 以便合并?
Thanks
谢谢
回答by Assassin
you can try to write to a response stream directly and output
您可以尝试直接写入响应流并输出
Response.Write("<script type=\"text/javascript\"> your JS call</script>");
Response.Write("<script type=\"text/javascript\"> your JS call</script>");
Regards, Szymon
问候, 席蒙
回答by Win
You can call a JavaScript function using registerStartupScript
.
您可以使用 .js 调用 JavaScript 函数registerStartupScript
。
ASPX Page
ASPX 页面
<script type="text/javascript">
function printOutput(arg) {
alert(arg);
}
</script>
Code Behind
背后的代码
protected void DoSomethingButton_Click(object sender, EventArgs e)
{
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(typeof (Page), "PrintScript_" + UniqueID, "printOutput('My HTML Code Generated by jquery function');", true);
}
回答by Richard Banks
You need to pass into the web.cs a reference to the page. You will then have access to the page.clientscript method
您需要向 web.cs 传递对页面的引用。然后您将可以访问 page.clientscript 方法