Passing value to a javascript function from DataBinder

  • Thread starter Thread starter Ronald Moolenaar
  • Start date Start date
R

Ronald Moolenaar

Hi,

I have found many postings where people wanted something like:

<asp:HyperLink NavigateUrl="javascript:MyFunction('<%#
DataBinder.Eval(Container.DataItem, "NeedComments") %>')"
RunAt="server">
</asp:HyperLink>

The problem seems to be that the DataBinding needs both ' and ", and
the rest of the stuff in NavigateUrl cannot contain another
apostrophe.

Proposed solutions in code behind will probably work, but when the
HyperLink is in an ItemTemplate of a DataList, I think it will be
difficult to do. After much struggling I found the following 'not code
behind' solution:

NavigateUrl='<%# String.Concat("javascript:DisplayInternetPage(", new
String((char)39, 1), DataBinder.Eval(Container.DataItem, "URL"), new
String((char)39, 1), ")") %>'

Cheers,
Ronald
 
JavaScript may use " or ', but not both to wrap string literals. If you are
using ' as the begin and ending control characters, then use: \' as an
escape sequence to safely have an apostrophe included in your string
literal.
 
Peter O'Reilly said:
JavaScript may use " or ', but not both to wrap string literals. If you are
using ' as the begin and ending control characters, then use: \' as an
escape sequence to safely have an apostrophe included in your string
literal.

I tried escaping " and ', but it doesn't work here because of the
databinding stuff. You get the "Server tag is not well formed" error.
 
Ronald Moolenaar said:
"Peter O'Reilly" <[email protected]!N!O!.S!P!AM!> wrote in message

I tried escaping " and ', but it doesn't work here because of the
databinding stuff. You get the "Server tag is not well formed" error.

It should work. Try assigning the databinding to a String variable/object
and use the Replace method of the String object.
 
Back
Top