Search code examples
javascript.netonclickaspxgridview

Javascript error occurs when trying to click on a gridview row


I have added a click event to the rows of a devexpress gridview on the server-side (VB.net).

 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

When executing, the bag value for e.Row.Attributes is:

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

It errors out on the client side when I attempt the click. The source shown in the chrome DevTools:

<tr id="gvSubNotes_DXDataRow0" class="dxgvDataRow" onclick="OnEntryClick(&#39;1375&#39;, &#39;Forager&#39;, &#39;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.&amp;nbsp; &amp;nbsp;i mean for subs.&amp;nbsp; yeah that&#39;s it.&#39;, &#39;DJANGO&#39;);" 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>

with the error "Uncaught SyntaxError: missing ) after argument list" at the first td

I've done this many times before. This is the first time I've run across this. Not sure where I messed up, so I need an extra set of eyes, if possible. Appreciate the help.


Solution

  • In your example the result of e.GetValue("ENTRY") returns a string that includes a single quote in the middle - "that's it" - which terminates the string after "that".

    Depending on what you expect the values to be you could either use double quotes

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

    or you need to clean up the string values before constructing the function call, maybe by getting rid of single quotes

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