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