2012-06-04 39 views
0

後面的代碼中添加jQuery時,輸入字符串的格式不正確我試着這樣做,它給出了我稱爲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); 
    } 
} 

回答

3

如果使用String.Format你需要逃避,你要因爲他們的Javascript代碼被逐字輸出的{}字符。要達到此目的,您分別使用{{}}

您可以閱讀更多關於字符串格式化的信息here,其中還解釋了由轉義花括號引起的奇怪行爲。

0

我認爲你需要躲避{功能後聲明類似

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