Johnn5er
Johnn5er

Reputation: 97

Javascript alert with "Sweet Alert" called from VB.Net code behind

In my application I am firing javascript alerts with Sweet Alerts from VB.Net code behind, on various page loads and events. I can get the basic alert firing no problem as shown below:

Dim script As String = String.Format("swal('Welcome , " + Username + "');")

ScriptManager.RegisterClientScriptBlock(Page, GetType(System.Web.UI.Page), "redirect", script, True)

There are other alerts available, which involve adding an object inside the quotes of the alert:

swal({   
         title: "Sweet!",   
         text: "Here's a custom image.",   
         imageUrl: "images/thumbs-up.jpg" 
    });

So my question is, when i want to use this in my code behind, im having trouble knowing where to add quotes in the right places, the object seems to cause a syntax error

Dim script As String = String.Formatswal({   title: "Sweet!",   text: "Here's a custom image.",   imageUrl: "images/thumbs-up.jpg" });

ScriptManager.RegisterClientScriptBlock(Page, GetType(System.Web.UI.Page), "redirect", script, True)

enter image description here

Upvotes: 0

Views: 3196

Answers (2)

haraman
haraman

Reputation: 2742

Its just a simple case of string concatenation containing quotes

Dim script As String = "<script type='text/javascript'>swal({   title: ""Sweet!"",   text: ""Here's a custom image."",   imageUrl: ""images/thumbs-up.jpg"" });</script>"

You can include double quotes in a string by escaping them with another double quote

Upvotes: 1

modal_dialog
modal_dialog

Reputation: 743

From the VB.NET/server-side perspective, none of the JavaScript is code, it's just a string to be emitted in the response. So to start, everything given to String.Format() must be in a VB.NET string, with double quotes:

Dim script As String = "just a string to VB";

From there on, you are just trying to get the contents of the string to be exactly what's need for valid JavaScript on the client. There are two ways to handle:

Anywhere inside the string where you need the quote character ("), use two-quotes instead (""):

Dim script As String = "<script>swal({title:""Sweet!"", text:""Here's a custom image."", imageUrl:""images/thumbs-up.jpg""});</script>"

OR, second way: JavaScript allows using EITHER single quote (') or double-quote ("). Since VB.NET doesn't consider single-quote a special character, just replace all your JavaScript double-quotes with single quotes:

Dim script As String = "<script>swal({title:'Sweet!', text:'Here's a custom image.', imageUrl:'images/thumbs-up.jpg'});</script>"

Lastly, String.Format is used to easily insert variable values into your string. It uses {0} {1} {2} etc. as substitution values. Then you pass in the variables as additional params. That's why you have to escape the {} chars. Here's an example:

Dim script As String = String.Format("<script>swal({{title:'{0}', text:'{1}', imageUrl:'{2}'}});</script>", strTitle, strText, strImageUrl)

Upvotes: 1

Related Questions