In
<%# Eval() #>
statements in asp.net, how to use quotes and double quotes when it's inside a tag element? The example below shows the solution:<td class="DataListRow">
<asp:HyperLink runat="server"
NavigateUrl='<%# Eval("RowID","return confirm('Load document with ID {0}?');") %>'
Text="View">
</asp:HyperLink>
</td>
This comes from a ListView and RowID is a key. The code needs to generate javascript to show a dialog with the unique row ID.
The answer is twofold.
One: enclose your tag element in single-quotes instead of double-quotes. This lets you use the double-quotes inside it for the eval. Then use
'
to render a single-quote.But what if I need double-quotes inside there too?
Now it's getting a little hairy. When ASP.NET processes this thing, it's going to actually take the formatted HTML inside the second Eval parameter to use as a format string. Then, that will be again processed when the Format function runs.
So you need to make sure that the HTML renders as a valid format string. Here's an example:
<td class="DataListRow">
<asp:HyperLink runat="server"
NavigateUrl='<%# Eval("DocumentTitle",
"confirm('Delete document with title \"{0}\"?');") %>'
Text="View">
</asp:HyperLink>
</td>
The
\"
will render as \"
, which is the correct format for asp.net to parse into just a double-quote in a string. Assuming all goes well, if DocumentTitle was "Some Document", you'll get a dialog that just says:Delete document with title "Some Document"?
No comments:
Post a Comment