Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server tag in OnClientClick

Tags:

asp.net

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?

like image 934
Richard Ev Avatar asked Feb 09 '09 13:02

Richard Ev


1 Answers

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.

like image 178
BlackMael Avatar answered Sep 21 '22 19:09

BlackMael