The following gives me an error of \"The server tag is not well formed\"
I found this answer over at www.asp.net
OnClientClick='<%# Eval("ProductName", "return confirm(""Delete the Product {0}?"")" ) %>'
This puts everything in the markup so anyone doing maintenance later doesn't have dig around to find all of the pieces.
Add the code dynamically in the ItemDataBound event for the ListView control.
In your page_Load event add the following
lst.ItemDataBound += new EventHandler<ListViewItemEventArgs>(lst_ItemDataBound);
Then in your ItemDataBound event handler add
Control DeleteButton = e.Item.FindControl("DeleteButton");
DeleteButton.OnClientClick = string.Format( "return confirm('Are you sure you want to delete '{0}'?", Username);
This solution should work whether you use OnClientClick or Sachin Gaur's solution.
You can add the onclick event at run time, like this:
DeleteButton.Attributes.Add("onclick", "'return confirm('Are you sure you want to delete '" + Username);
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.