Message box in asp.net website using JavaScript

Introduction: In previous articles i explained How to show message box and redirect to another page/website in visual studio using JavaScript. and How to call java script function from code behind file in asp.net and How to disable Right click on asp.net website page using JavaScript and JavaScript validations in asp.net website
In this article i  am going to explain how to show Message box in asp.net website using JavaScript.I will also mentioned how to solve the error Message box not working in UpdatePanel. One of the frequent requirement in asp.net application is to show some message or result in message box. In this article i have written the code to show message box in asp.net application using JavaScript.

C#.Net Code to show Message box in asp.net website using JavaScript

 In the code behind file(.aspx.cs) write the code as:

  protected void btnMsg_Click(object sender, EventArgs e)
    {
 ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Record saved successfully');", true);
    }

OR
  • include namespace in the code behind(.aspx.cs) file as:
using System.Web.Script.Serialization;

  protected void btnMsg_Click(object sender, EventArgs e)
    {
var message = new JavaScriptSerializer().Serialize("Record saved successfully");
        var script = string.Format("alert({0});", message);
        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "", script, true);
    }

Note: If you want to show message inside UpdatePanel then the above code will not work. Use the below code instead

  ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Record saved successfully');", true);       
 
VB.Net Code to show Message box in asp.net website using JavaScript

 In the code behind file(.aspx.vb) write the code as:

 Protected Sub btnMsg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMsg.Click
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),      Guid.NewGuid().ToString(), "alert('Record saved successfully');", True)
End Sub

OR
  •  Include namespace in the code behind file(.aspx.vb) as:
Imports System.Web.Script.Serialization

Protected Sub btnMsg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMsg.Click
  Dim message = New JavaScriptSerializer().Serialize("Record saved successfully")
                Dim script = String.Format("alert({0});", message)
                ScriptManager.RegisterClientScriptBlock(Page, Page.[GetType](), "", script, True)
End Sub

Note: If you want to show message inside UpdatePanel then the above code will not work. Use the below code instead

ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Message", "alert('Record saved successfully');", True)

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."

Previous
Next Post »

5 comments

Click here for comments
Anonymous
admin
September 13, 2013 ×

tnx. it helps me

Reply
avatar
September 13, 2013 ×

i am glad to know that my article helped you..keep reading

Reply
avatar
rohidas
admin
March 11, 2014 ×

Ur doing gr8 job. Keep it up

Reply
avatar
March 14, 2014 ×

Hello rohidas..thanks for appreciating my work..i am glad you found this article helpful..stay connected and keep reading..:)

Reply
avatar
June 20, 2015 ×

Good work , but still need to try it

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..