ASP.Net C# Format Javascript Delete Confirmation Alert Containing Apostrophes

How to format a javascript delete confirmation alert with text containing apostrophes.

ASP.Net Button with OnClientClick

<asp:Button ID="Delete" runat="server" Text="Delete" OnClick="Delete_Function" OnClientClick='<%# FormatDeleteAlert() %>' CssClass="buttonText" />

Javascript Alert Function

protected string FormatDeleteAlert()
  string lineitem = "";
  lineitem = Eval("ItemName").ToString().Replace("'", "\\'");
  StringBuilder sConfirm = new StringBuilder();
  sConfirm.Append("return confirm('Are you sure you want to delete " + lineitem + "?');");
  return sConfirm.ToString();

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s