tags:

views:

2093

answers:

5

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?

A: 

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);


Sachin Gaur
If you have a server side event for this button, I believe this strategy will add 'onclick' to the HTML element twice, causing unexpected behaviour.
John MacIntyre
No it won't add. A server side event will have some __dpPostBack... call. You can add the attribute and view the page source.
Sachin Gaur
Sadly I cannot do this - as this control is part of a databound ListView
Richard Ev
My mistake. ... sorry, I'm positive I've had this issue in the past. I guess I did not make the correct distinction.
John MacIntyre
I think, You can do this in the List Bound event as we have the RowDataBound event for GridView.
Sachin Gaur
A: 

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.

John MacIntyre
Sadly I cannot do this - as this control is part of a databound ListView
Richard Ev
@Richard E-If I understand you correctly it should be in your 'e' parameter.
John MacIntyre
@Richard E-I've adjusted the answer to help you find your button. I think we deserve our down vote to be removed. Thanks.
John MacIntyre
+9  A: 

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.

BlackMael
Note that CreateConfirmation(Eval("Username")) should be written CreateConfirmation((string)Eval("Username"))(I'm using C#...might be that VB.NET automatically casts the object returned from Eval)
Richard Ev
If you're using C#, you could just make the 'Username' parameter type object opposed to string, and you wouldn't need the cast
John
BlackMael, You saved me a lot of time, thanks !
Tony
A: 

Hi,

I have another question about the answer provided above i.e.

OnClientClick='<%# CreateConfirmation(Eval("Username")) %>'

I have read that <%# %> is a databinding expression, but overhere we are not directly data-bidning (infact returining value from the function CreateConfirmation) and I also thought that it should work with <%= %> but it gives JavaScript error message i.e. Illigal XML character i.e. =

Please could you clarify as to why is this?

Many Thanks.

+3  A: 

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.

MJohn
This is almost correct. Either OnClientClick='<%# Eval("ProductName", @"return confirm(""Delete the Product {0}?"")" ) %>' or OnClientClick='<%# Eval("ProductName", "return confirm(\"Delete the Product {0}?\")" ) %>' worked for me; basically the inner-most double-quote is a double-quote in a C# string and should be escaped accordingly.
Kit