Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试在代码隐藏中添加jQuery时,输入字符串的格式不正确_C#_Asp.net - Fatal编程技术网

C# 尝试在代码隐藏中添加jQuery时,输入字符串的格式不正确

C# 尝试在代码隐藏中添加jQuery时,输入字符串的格式不正确,c#,asp.net,C#,Asp.net,我尝试了一下,它在我调用的String.Format行中给出了标题中提到的错误 public static void JqueryDialogue(string divId) { String script = String.Format( "$(document).ready(function(){ $('#{0}').dialog('open'); });", divId); // Gets the executing web page

我尝试了一下,它在我调用的
String.Format
行中给出了标题中提到的错误

public static void JqueryDialogue(string divId)
{
    String script = String.Format(
        "$(document).ready(function(){ $('#{0}').dialog('open'); });", 
        divId);

    // Gets the executing web page
    Page page = HttpContext.Current.CurrentHandler as Page;
    string codeId = "openDialoge" + divId.ToString();

    // Checks if the handler is a Page and that the script isn't already on Page
    if (page != null && !page.ClientScript.IsClientScriptBlockRegistered(codeId))
    {
        page.ClientScript.RegisterStartupScript(
            typeof(JavascriptHelper), 
            codeId, 
            script,
            true);
    }
} 

如果使用
String.Format
,则需要转义希望按字面形式输出的
{
}
字符,因为它们是Javascript代码。要实现这一点,请分别使用
{{
}


您可以阅读有关字符串格式的更多信息,其中还解释了由于转义大括号而导致的奇怪行为。

如果使用
string.Format
则需要转义希望按字面形式输出的
{
}
字符,因为它们是Javascript代码。要实现这一点,请分别使用
{{
}


您可以阅读有关字符串格式的更多信息,其中还解释了由于转义大括号而导致的奇怪行为。

我认为您需要在函数声明之后转义
{
,类似于

String script = String.Format("$(document).ready(function()
{{ $('#{0}').dialog('open'); }});", divId);

我认为您需要在函数声明之后转义
{
,比如

String script = String.Format("$(document).ready(function()
{{ $('#{0}').dialog('open'); }});", divId);