The following gives me an error of "The server tag is not well formed"
<asp:LinkButton ID="DeleteButton" runat="server" CommandName="Delete"
OnClientClick="return confirm('Are you sure you want to delete <%# Eval("Username") %>?');">
Delete
</asp:LinkButton>
(This is used in a data bound ListView that displays a list of users. When you click the delete button a JavaScript confirm dialog is used to ask you if you're sure)
So, how can I embed a server tag in a string that contains JavaScript?
The problem is the binding nugget and the use of single and double quotes.
<asp:LinkButton D="DeleteButton" runat="server" CommandName="Delete" OnClientClick='<%# CreateConfirmation(Eval("Username")) %>'>Delete</asp:LinkButton>
Then on the code-behind add the function...
Public Function CreateConfirmation(ByVal Username As String) As String
Return String.Format("return confirm('Are you sure you want to delete {0}?');", Username)
End Function
When the binding nugget is used as the value for an attribute, you'll note you have to use single quotes. Your script also needed quotes for the embedded string parameter to the confirm function. You basically ran out of quotes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With