tags:

views:

380

answers:

4

I am trying to bind an ASP.NET hyperlink to the "Handle" column of my dataset like this:

<ItemTemplate>
    <asp:HyperLink ID="idTracking" runat="server"  NavigateUrl='<%# "javascript:SendPath(" + Eval( "Handle", "{0}") + ")" %>' Text="Test" />                                            
</ItemTemplate>

I would like the NavigateUrl to read:

javascript:SendPath('123abc')

However, I cannot introduce the single quotes. How do I do this?

+2  A: 

As far as I know the text property of the hyperlink control automatically encodes, I've always has to simply do it as a standard html anchor tag.

So something like this.

<a href='<%# "javascript:SendPath(" + Eval( "Handle", "{0}") + ")" %>'>Your Link</a>
Mitchel Sellers
+1  A: 

why not do this:

 <asp:HyperLink ID="idTracking" runat="server"  NavigateUrl='<%#  Eval("Handle", "javascript:SendPath(\'{0}\')") %>' Text="Test" />
Richard
This doesn't put the quote out to the user.
Mitchel Sellers
+1  A: 

Be sure to escape any single quotes in the string you're putting into the {0} spot, too. My suggestion would be to use an HTML anchor tag rather than an asp:HyperLink control like such:

<asp:TemplateColumn ...>
  <ItemTemplate>
    <a href="javascript:SendPath('<%#Container.DataItem("Handle").ToString.Replace("'", "\'") %>');">Test</a>
  </ItemTemplate>
</asp:TemplateColumn>
Brandon Montgomery
A: 

Converting to a standard html anchor tag will generally do the trick. However, if you must use an asp control maybe because of other dependencies, \x0027 will not interfere with the asp syntax.

<asp:HyperLink ID="lnkDelete" runat="server" NavigateUrl='<%# "return confirm(\x0027Delete " + Eval(Container.DataItem, "name") + "?\x0027)" %>' Text="Delete" />