2011-02-15 27 views
0

从我的代码后面我传递这对客户端脚本如何在JavaScript

C#代码背后 * 注:如果我的评论下面对付“&”,“(”,“)”线比我没有得到js错误 *

if (btnDelete != null) 
{ 
    btnDelete.Attributes["onclick"] = String.Format("return DeleteRow('{0}', '{1}', '{2}', '{3}');", e.Row.ClientID, e.Row.RowIndex, DataBinder.Eval(e.Row.DataItem, "Id"), DataBinder.Eval(e.Row.DataItem, "Name")); 
} 

//javascript 
function DeleteRow(rowId, rowIdx, Id, Name) {  
    var result = confirm('Are you sure you want to delete this record? \n\n Id: ' + Id + "\n Name: " + Name); 

    if (!result) 
     HighlightRow(rowId, rowIdx % 2 ? "#DEEEE9" : "#FFFFFF"); 
    return result; 
} 

错误消息:

Message: Unterminated string constant 
Line: 1280 
Char: 180 
Code: 0 
URI: http://localhost:4964/admin/default.aspx 


Message: Unterminated string constant 
Line: 1341 
Char: 178 
Code: 0 
URI: http://localhost:4964/default.aspx 


Message: Expected ')' 
Line: 1401 
Char: 152 
Code: 0 
URI: http://localhost:4964/default.aspx 
+2

难道这就是[同样的问题之前(http://stackoverflow.com/questions/4997634/how-to-escape-逗号在JavaScript的)? – user113716 2011-02-15 02:30:20

回答

0

这里是我阿贝尔解决它:

btnDelete.Attributes["onclick"] = String.Format("return DeleteRow('{0}', '{1}', '{2}', '{3}');", e.Row.ClientID, e.Row.RowIndex, DataBinder.Eval(e.Row.DataItem, "Id"), DataBinder.Eval(e.Row.DataItem, "Name").ToString().Trim().Replace("'", "\\'"))); 
2

很难从你的问题告诉,但听起来好像正在返回的包括单引号字符值(撇号)这将结束您的字符串在生成的JavaScript。

正在为抛出错误的行生成的代码是什么?

当类似的事情发生在我身上这导致类似:

return DeleteRow('1', '2', 'abscde', 'Mc'Adams'); 

这将导致因为值Mc'Adams的错误。如果是这种情况,那么你将不得不通过一种方法来发送你的数据,这些方法会跳过那些会破坏你的javascript的值。

1

你已经使用了混合的单引号和双引号。在这里改变行;

var result = confirm('Are you sure you want to delete this record? \n\n Id: ' + Id + '\n Name: ' + Name) 
+0

没有帮助....... – 2011-02-15 14:12:02