简体   繁体   中英

Passing a string value from c# file to js file

I am trying to pass a string value from c# file to js file.

If I try to pass an int value, then I can pass it, but I am unable to pass string value.

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

js file

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.

First version:

onclick="F1(abc)"

Second version:

onclick="F1(5)"

Clearly the second version is passing the value 5 . The first version is passing the value of abc - whatever that is, within the context of the Javascript you're executing.

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 .

For example, to get the string abc in your call, you want the attribute to be:

onclick="F1('abc')"

but if to get the string I don't know in your call, you want the attribute to be:

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. Once you've worked out what HTML you want to generate, writing the code to do so is often relatively simple.

Try adding single-quotes around the value when building it in your C# string. In your first scenario, the Js is receiving:

F1(abc);

Which it reads as being the variable abc. Try adding single quotes (and it's probably best to use 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) . That would interpret abc as a variable name, not a string literal.

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 .

You should use

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

So the output will be

F1("abc")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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