0

How can I escape the Quotes so that this statement

string sScript =@"<script language='javascript'>function ShowDropDown(){var combo = $find("""+this.ClientID+""");combo.showDropDown(true);}</script>";

reads like this

function ShowDropDown() {
                var combo = $find("ctl00_ctl00_MainContent_MainContent_VendorTypeIdComboBox");
                combo.showDropDown(true);
            }

EDIT- UPDATE I might of asked the question wrong because i keep getting different errors. If I put the javascript directly on the page normally the function works. When I inject the javascript this way it doesnt work

I am doing this in code behind

string sScript =@"<script language='javascript'> function ShowDropDown(){  var combo = $find("""+this.ClientID+@"""); combo.showDropDown(true); } </script>";
        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "autoopendropdown", sScript, false);

        OnClientFocus = "ShowDropDown()";

it gets generated this way

<script language='javascript'> function ShowDropDown(){  var combo = $find("ctl00_ctl00_MainContent_MainContent_VendorTypeIdComboBox"); combo.showDropDown(true); } </script>

but the variable combo is null and thats what the problem is. I cant figure out why when it is registered with code-behind it doesnt work and when write it normally on the page it does.

ChampChris
  • 1,565
  • 5
  • 26
  • 44
  • Life may be easier if you just use single quotes in JavaScript... Also you need to make sure there is no chance to get quotes in variables you are concatenating - you may need to add something like "escape all quotes in an argument" method... – Alexei Levenkov Mar 22 '13 at 00:04
  • Everyone is getting hung up on the C# quoting. It's valid. He's outputting javascript from C#. – rein Mar 22 '13 at 00:07
  • yes, the question is "reads like this" which is ignored in most answers here. @rein, your answer provides one way to do this. – Mark Schultheiss Mar 22 '13 at 00:24
  • The line breaks are a matter of format in the question so it was not all on one line. The question literally states "How can I escape the Quotes" and will fail at `+"""` with the error "; expected". – Travis J Mar 22 '13 at 00:29
  • @TravisJ - I'm not sure where you're seeing that the error "; expected" is raised. From the updated question text this does not seem to be the case. – rein Mar 22 '13 at 00:34
  • @rein - That was from the original question. The updated question will execute without error. – Travis J Mar 22 '13 at 00:36
  • I am getting am error chrome and IE. when inspect the page in Chrome combo is undefined. When I build and run from VS2012 and target IE it crashes saying the same thing before the page even finishes building. – ChampChris Mar 22 '13 at 00:52
  • possible duplicate of [In C#, can I escape a double quote in a literal string?](http://stackoverflow.com/questions/1928909/in-c-can-i-escape-a-double-quote-in-a-literal-string) – hjpotter92 Mar 22 '13 at 02:19
  • @ChampionChris - just to be clear you DO have a function in your script elsewhere literally named "$find"? and if you replace "language='javascript'" with "type='text/javascript'" it does not change the result? – – Mark Schultheiss Mar 22 '13 at 12:36

6 Answers6

1

Simple way: Add the same @ at the beginning of the second string literal:

string sScript =@"<script language='javascript'>function ShowDropDown(){var combo = $find("""+this.ClientID+@""");combo.showDropDown(true);}</script>";

Better way: use string.Format

string sScript = string.Format(
@"<script language='javascript'>
    function ShowDropDown(){
        var combo = $find(""{0}"");combo.showDropDown(true);
    }
</script>",
    this.ClientID);

(Best way: separate concerns using unobtrusive javascript.)

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
1
string sScript = "<script language='javascript'>\n" +
                 "function ShowDropDown() {\n" +
                 "    var combo = $find(""" + this.ClientID + """);\n" +
                 "    combo.showDropDown(true);\n" +
                 "}\n" +
                 "</script>";
rein
  • 32,967
  • 23
  • 82
  • 106
  • yuck was my first thought here – Mark Schultheiss Mar 22 '13 at 00:07
  • It's not pretty but without a templating language you don't have much choice. – rein Mar 22 '13 at 00:08
  • Yes, it works, but also see my answer using a string literal. Gets the same result of the "format/looks like" requirement which is missing in lots of answers. We shall let the OP determine which is best methinks. :) – Mark Schultheiss Mar 22 '13 at 00:20
  • @Mark: I'm torn between the two approaches (yours and mine). Mine's ugly and yours has the unfortunate side effect that it messes up the program indentation in the C# file, so it's hard to align statements before and after the string assignment. – rein Mar 22 '13 at 00:30
0

The escape for double quotes in C# (and most C family languages) is \"

Or you could just use single quotes since it's valid in JavaScript.

System Down
  • 6,192
  • 1
  • 30
  • 34
0

If I understand your question correctly, you want to concatenate this.ClientID with the rest of the script.

You can do this using the String.Format method like so:

string scriptFormat = @"<script language='javascript'>function ShowDropDown(){var combo = $find(""{0}"");combo.showDropDown(true);}</script>";
string sScript = String.Format(scriptFormat, this.ClientID);

Note that inside a verbatim string literal, "" produces a single " character.

Jason Watkins
  • 3,766
  • 1
  • 25
  • 39
0

You can escape them using the \ character.

For a complete list of escape combinations, see section 2.4.4.4 Character literals of the C# language specification.

Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
0

NOTE: language is deprecated for script tags, use type

string sScript =@"
<script type='text/javascript'>
function ShowDropDown(){
                var combo = $find(""" + this.ClientID + @""");
                combo.showDropDown(true);
            }
</script>";
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100