How to display JavaScript alert in SharePoint visual web part

This SharePoint tutorial, we will discuss how to show or display JavaScript alert in SharePoint 2013/2016 visual web part.

Here, I would like to enlighten your coding skills and enhance your application user-friendly messaging. Any Web user expects a success/error to be as an alert message, but the challenge here is that how we show the alert in a catch block of code behind in SharePoint visual webpart / classic webpart.

Here we will see how we can display JavaScript alert box inside a catch block in SharePoint visual web part using C#.Net in SharePoint 2013/2016.

In the SharePoint code-behind, we will not be able to get the javascript object to show an alert with an error message.

Display JavaScript Alert in SharePoint Visual Web Part

Use the below single line to show the error message in C# code behind in SharePoint.

ScriptManager.RegisterStartupScript(this, GetType(), "flash", "alert(‘Error Message');", true);

The complete code will look like this:

try
{
SPSite objSite = new SPSite("http://sharepoint13:1245"); // I am trying to get the object of a site collection out side the FARM
}
catch(Exception ex)
{
ScriptManager.RegisterStartupScript(this, GetType(), "flash", "alert(‘ You can not get the object out side this FARM');", true);
}

Note: In the above code block I am trying to get an object of a site collection outside the FARM.

You may like following SharePoint visual web part tutorials:

This SharePoint tutorial, we learned how to show JavaScript alert in visual webpart in SharePoint 2013/2016.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>