简体   繁体   中英

escaping characters

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.

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.)

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

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.

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.

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 .

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>";

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