Search code examples
javascriptasp.netdatalist

DataBind with single quoted string is not calling javaScript function


Scenario:- I've a Data List, loaded with with few images. Each image has its own distinct sting property ("Description") which is loaded from a database query. Now when there is a mouse-hover on any image, it calls a already defined OnClinetClick JS function. Thats JS function's parameter binds the "Description" of currently mouse hovered image. (pls refer the code)

Problem:- Whenever the "Description" has a single quote in it (ex. World's Best), "demoJS" is not called.But if the same "Description" has normal string in it (ex. World Best), "DemoJS" is called succesfully.

Please let me know if any one can guide on this.

 <asp:DataList ID="ItemSelectionDataList" CssClass="itemSelectionDataList" runat="server"
                 DataSourceID="DemoDataSource">
     <ItemTemplate>
         <a href="DemoPage.aspx?1>"
             class="itemSelectionItemLink" 
             onmouseover="demoJS('<%# DataBinder.Eval(Container.DataItem, "Description")%'                                   
         </a>
     </ItemTemplate>
</asp:DataList>

Solution

  • Why not just replace that ' with \' like this ??

    DataBinder.Eval(Container.DataItem, "Description").ToString().Replace("'", "\\'")