Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Monday, 2 February 2015

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.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Record saved successfully');"True)

0 comments:

Post a Comment