user38230
user38230

Reputation: 665

How to insert single quotes on a databound asp:hyperlink field in asp.net?

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?

Upvotes: 1

Views: 2563

Answers (6)

Vishal Suthar
Vishal Suthar

Reputation: 72

Write it like this:

NavigateUrl='<%# "SendPath(\"" + Eval( "Handle", "{0}") + "\")" %>'

Working Example:

Note : \" is written " in the Tag.

Output

NavigateUrl='SendPath("123")'

Upvotes: 1

Nick DeVore
Nick DeVore

Reputation: 10166

I was trying to figure this out myself. Here is what did that appears to work:

onmouseup=<% String.Format("alert('{0}')",Eval("Code"))  %>

Note the missing single quotes at the beginning. Otherwise, the ASP.NET parser will choke on the single quote that follows the single quote after the = sign. The HTML syntax checker underlines the text in green and says "Attribute Values Must Be Enclosed In Quotes" but it works out in the generated html.

Upvotes: 0

Briankc
Briankc

Reputation: 1

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

Upvotes: 0

Brandon Montgomery
Brandon Montgomery

Reputation: 6986

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>

Upvotes: 1

Richard
Richard

Reputation: 22026

why not do this:

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

Upvotes: 1

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

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>

Upvotes: 2

Related Questions