0

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

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much for your help but i have tried both these techniques and both have a compile error "Input string was not in a correct format."
1

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

8 Comments

Thanks for your response, but i tried that, it gve me a compile error:System.FormatException: Input string was not in a correct format.
Try using it without String.Format. Just a simple string assignment.
Yes, get rid of String.Format because the {} are used by it. You'd have to escape with {{ and }}. Just do the simple string assignment. I'll edit my answer to reflect that too.
Thanks for the info, but it seems ScriptManager.RegisterClientScriptBlock(Page, GetType(System.Web.UI.Page), "redirect", script, True) wont work without it
@Johnn5er, you're script block needs to be wrapped in the <script> tags. It does not add them for you. That may be part of your problem. I'll edit my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.