Show Alert Message Box from Code-behind in Asp.net C#

Category > ASP.NET || Published on : Thursday, June 25, 2015 || Views: 7726 || Show Alert Message Box from Code-behind in Asp.net C# Alert Message Box from Code-behind


Step 1: Create a new webform in Visual Studio 2010, with the following codes:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test3.aspx.cs" Inherits="test3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>    
</head>
<body>
    <form id="form1" runat="server">
        <div>

        </div>
    </form>
</body>
</html>

Step 2: Add the below javascript code in the Head section of the page:-

<script type="text/javascript">
        function alertMessage() {
            alert('JavaScript Function Called!');
        }
    </script>

Step3: Add three asp.net buttons in the body of the page as below:-

 <h4>Show alert message from code-behin in Asp.net</h4>
            <div>
                <asp:Button ID="btnClientSide" runat="server" OnClientClick="alertMessage()"
                    Text="Client-side" />
                <asp:Button ID="btnServerSideMethod1" runat="server" Text="Server-side(Method1)"
                    OnClick="btnServerSideMethod1_Click" />
                <asp:Button ID="btnServerSideMethod2" runat="server" Text="Server-side(Method2)"
                    OnClick="btnServerSideMethod2_Click" />
            </div>

step 4: Following is the C# code to display or show alert message:-


 protected void btnServerSideMethod1_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", "alertMessage();", true);
    }

    protected void btnServerSideMethod2_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage",
        "alert('Called from code-behind directly!');", true);
    }

If you are dealing with asp.net UpdatePanel and UpdateProgress, use the following code:

//Following statement is used to call pre-defined javascript function
protected void btnServerSide_Click(object sender, EventArgs e)
{
  ScriptManager.RegisterStartupScript(myUpdatePanelID, myUpdatePanelID.GetType(), "myAlert",
  "alert('Called from code-behind directly!');", true);
} 

Complete aspx page source codes for how Alert Message Box from Code-behind in Asp.net C#(below)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test3.aspx.cs" Inherits="test3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function alertMessage() {
            alert('JavaScript Function Called!');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h4>Show alert message from code-behin in Asp.net</h4>
            <div>
                <asp:Button ID="btnClientSide" runat="server" OnClientClick="alertMessage()"
                    Text="Client-side" />
                <asp:Button ID="btnServerSideMethod1" runat="server" Text="Server-side(Method1)"
                    OnClick="btnServerSideMethod1_Click" />
                <asp:Button ID="btnServerSideMethod2" runat="server" Text="Server-side(Method2)"
                    OnClick="btnServerSideMethod2_Click" />
            </div>
        </div>
    </form>
</body>
</html>

Complete Code Behind source codes for how Alert Message Box from Code-behind in Asp.net C#(below)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnServerSideMethod1_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", "alertMessage();", true);
    }

    protected void btnServerSideMethod2_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage",
        "alert('Called from code-behind directly!');", true);
    }
}

Conclusion, In this tutorial we have learned to Show Alert Message Box from Code-behind in Asp.net C#. Happy coding!!!