如何将c#函数返回值作为参数传递给Javascript [英] how to pass c# function return value as parameter to Javascript
本文介绍了如何将c#函数返回值作为参数传递给Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个javascript函数JA接受一个字符串参数,而ac#函数CB接受字符串参数
例如
< br $>
// js
功能JA(位置)
{
} < br $>
// c#
公共字符串计算(字符串标准)
{
// proccess and return string
}
现在我想要调用我的javascript函数即JA,这样我将c#的返回值作为参数传递给这个javascript函数
ie
< script> ;
JA(计算())
< / Script>
i have a javascript funtion JA which accepts a string parameter, and a c# function CB which accepts string parameter
e.g
//js
function JA(location)
{
}
//c#
public string calculate(string par)
{
//proccess and return string
}
now i want to call my javascript function i.e JA such that i pass the return value of c# as parameter to this javascript function
i.e
<script>
JA(calculate())
</Script>
推荐答案
public static void calculate(string par)
{
// Cleans the message to allow single quotation marks
string cleanMessage = par.Replace("'", "\\'");
string script = string.Format("<script type=\"text/javascript\">
(document).ready(function() {JA('{0}');});< / script>,cleanMessage);
// 获取正在执行的网页
Page page = HttpContext.Current.CurrentHandler as Page;
// 检查处理程序是否为Page并且脚本未在页面上全部准备
if (page!= null &&!page.ClientScript.IsClientScriptBlockRegistered ( JA))
{
page.ClientScript.RegisterClientScriptBlock(< span class =code-keyword> typeof (< your class = < span class =code-string> name = > ), JA,脚本);
}
} < / 您的 >
(document).ready(function(){JA('{0}');});</script>", cleanMessage); // Gets the executing web page Page page = HttpContext.Current.CurrentHandler as Page; // Checks if the handler is a Page and that the script isn't allready on the Page if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("JA")) { page.ClientScript.RegisterClientScriptBlock(typeof(<your class="" name="">), "JA", script); } }</your>
您可以使用ajax jquery
You can using ajax jquery
function JA(location)
{
这篇关于如何将c#函数返回值作为参数传递给Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文