我在服务器端(VB.net)的 devexpress gridview 的行中添加了一个单击事件。
Protected Sub gvSubNotes_HtmlRowPrepared(sender As Object, e As ASPxGridViewTableRowEventArgs)
e.Row.Attributes.Add("onclick", String.Format("OnEntryClick('{0}', '{1}', '{2}', '{3}');", e.GetValue("ID"), e.GetValue("SYNOPSIS"), e.GetValue("ENTRY"), e.GetValue("ENTERED_BY")))
End Sub
执行时,e.Row.Attributes的bag值为:
OnEntryClick('1375', 'Forager', 'UPDATED on 9/12/2023 8:39:07 AM: UPDATED on 9/12/2023 8:36:32 AM: Trying to scrape up a sub for lunch. i mean for subs. yeah that's it.', 'DJANGO');
当我尝试单击时,客户端出错。 chrome DevTools 中显示的源码:
<tr id="gvSubNotes_DXDataRow0" class="dxgvDataRow" onclick="OnEntryClick('1375', 'Forager', 'UPDATED on 9/12/2023 8:39:07 AM: UPDATED on 9/12/2023 8:36:32 AM: Trying to scrape up a sub for lunch.&nbsp; &nbsp;i mean for subs.&nbsp; yeah that's it.', 'DJANGO');" onmouseover="OnEntryMouseOver(this);" onmouseout="OnEntryMouseOut(this);" style="background-color:White;">
<td class="dxgv" style="color:Black;background-color:Beige;">9/11/2023</td> <!--ERROR HERE -->
<td class="dxgv dx-ellipsis" style="color:Black;">Forager</td>
<td class="dxgvPHEC dxgv" style="color:Black;background-color:Beige;">DJANGO</td>
<td class="dxgvHEC"></td>
</tr>
出现错误“Uncaught SyntaxError:参数列表后缺少 )” 在第一个td
我以前已经做过很多次了。这是我第一次遇到这种情况。不知道我哪里搞砸了,所以如果可能的话,我需要一双额外的眼睛。感谢您的帮助。
在您的示例中,e.GetValue("ENTRY") 的结果返回一个字符串,其中在中间包含一个单引号 - “that's it” - 在“that”之后终止该字符串。
根据您期望的值,您可以 使用双引号
e.Row.Attributes.Add("onclick", String.Format("OnEntryClick(\"{0}\", \"{1}\", \"{2}\", \"{3}\");", e.GetValue("ID"), e.GetValue("SYNOPSIS"), e.GetValue("ENTRY"), e.GetValue("ENTERED_BY")))
或者您需要在构造函数调用之前清理字符串值,也许可以去掉单引号
e.Row.Attributes.Add("onclick", String.Format("OnEntryClick('{0}', '{1}', '{2}', '{3}');", e.GetValue("ID"), e.GetValue("SYNOPSIS").replace("'", ""), e.GetValue("ENTRY").replace("'", ""), e.GetValue("ENTERED_BY")))