简体   繁体   English

将字符串值从c#文件传递到js文件

[英]Passing a string value from c# file to js file

I am trying to pass a string value from c# file to js file. 我试图将一个字符串值从c#文件传递给js文件。

If I try to pass an int value, then I can pass it, but I am unable to pass string value. 如果我尝试传递一个int值,那么我可以传递它,但我无法传递字符串值。

string value = "abc";
 int a=5;
 TableCell.Attributes.Add("onclick", "F1("+value +")"); //NOTHING HAPPENS
 TableCell.Attributes.Add("onclick", "F1("+a +")"); //Works Perfectly

js file js文件

function F1(value) {
    alert(value);
}

Pass string value in quotes '' 将字符串值传递给引号''

Use 采用

TableCell.Attributes.Add("onclick", "F1('"+value +"')"); 
                                        ^          ^

Otherwise it treated as variable. 否则将其视为变量。 Currently you must be getting error in browser console. 目前,您必须在浏览器控制台中收到错误。

Consider what your HTML will look like. 考虑一下HTML的外观。

First version: 第一版:

onclick="F1(abc)"

Second version: 第二版:

onclick="F1(5)"

Clearly the second version is passing the value 5 . 显然,第二个版本传递的值为5 The first version is passing the value of abc - whatever that is, within the context of the Javascript you're executing. 第一个版本传递abc的值 - 无论是什么,在您正在执行的Javascript的上下文中。

You could quote the string, making sure that you escape quotes etc - I'm assuming that in reality, your value is fetched dynamically from somewhere, and you might not have much control over the content. 可以引述字符串,并确保你逃避报价等等-我假设在现实中,你的价值是从什么地方获取的动态,而你可能没有对内容的控制程度。 Hopefully there's something within whatever ASP.NET version you're using that will let you do that, such as HttpUtility.JavaScriptStringEncode . 希望你正在使用的任何ASP.NET版本都可以使用它,例如HttpUtility.JavaScriptStringEncode

For example, to get the string abc in your call, you want the attribute to be: 例如,要在调用中获取字符串abc ,您希望该属性为:

onclick="F1('abc')"

but if to get the string I don't know in your call, you want the attribute to be: 但如果要在通话中获取I don't know的字符串,您希望该属性为:

onclick="F1('I don\'t know')"

The key is to look at the generated HTML - pretend you're the browser, and look at the world from its perspective. 关键是要看看生成的HTML - 假装你是浏览器,从它的角度看世界。 Once you've worked out what HTML you want to generate, writing the code to do so is often relatively simple. 一旦你弄清楚你想要生成什么HTML,编写代码通常会相对简单。

Try adding single-quotes around the value when building it in your C# string. 尝试在C#字符串中构建单引号时添加单引号。 In your first scenario, the Js is receiving: 在您的第一个场景中,Js正在接收:

F1(abc);

Which it reads as being the variable abc. 它读作变量abc。 Try adding single quotes (and it's probably best to use string.format, BTW): 尝试添加单引号(最好使用string.format,BTW):

TableCell.Attributes.Add("onclick", string.Format("F1('{0}')", value));

When you use the string, it would produce the JavaScript code F1(abc) . 当您使用该字符串时,它将生成JavaScript代码F1(abc) That would interpret abc as a variable name, not a string literal. 这会将abc解释为变量名,而不是字符串文字。

Add apostrophes around the string to make it a string literal: 在字符串周围添加撇号以使其成为字符串文字:

TableCell.Attributes.Add("onclick", "F1('" + value + "')");

If the string can contain apostrophes or backslashes, you would need to escape them: 如果字符串可以包含撇号或反斜杠,则需要将它们转义:

TableCell.Attributes.Add("onclick", "F1('" + value.Replace("\\", "\\\\").Replace("'", "\\'") + "')");

这将负责逃避任何特殊字符(即引号等...)

TableCell.Attributes.Add("onclick", "F1('"+HttpUtility.JavaScriptStringEncode(value)+"')"); 

如果您在参数中传递字符串,则需要使用“或”字符来分隔它。您的变量名称在调用中转换为(adb)。(“adb”)或('adb')将是字符串值。

That's because it will print 那是因为它会打印出来

F1(abc)

So it will look for a variable called abc . 所以它会寻找一个名为abc的变量。

You should use 你应该用

TableCell.Attributes.Add("onclick", "F1(\""+value +"\")");

So the output will be 所以输出将是

F1("abc")

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

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