Search code examples
javajqueryasp.netevallinkbutton

Uncaught ReferenceError: XXX is not defined with specific value


I get the following exception when i click on the following link :

Uncaught ReferenceError: G502 is not defined


<asp:LinkButton ID="lkSelect" runat="server" Font-Names="Tahoma" Font-Size="8" OnClientClick='<%# "CallParent(" +Eval("l_room_no").ToString() + " );" %>'><%# Eval("Cell_Data") %></asp:LinkButton>

Only when :

 Eval("l_room_no") = G502

my script function :

 function CallParent(room) {
          //  window.opener.DrawPaths(room);
            // window.close();
            alert(room);
        }

Solution

  • The argument of CallParent should be enclosed between quotes. However, since the data binding expression already uses single and double quotes, you can pass the converted character code to string.Format:

    <asp:LinkButton ... OnClientClick='<%# string.Format("CallParent({0}{1}{0});", (char)39, Eval("l_room_no")) %>' Text='<%# Eval("Cell_Data") %>' />
    

    This example assumes that you use C#. The VB.NET equivalent of (char)39 would be chr(39).