Alert(“Message”) box in Asp.Net Codebehind and Call java fuction from codebehind

In highly interactive websites and intranet sites, you probably want to let the users know what’s going on when they delete, save, export etc. on the site. Those kinds of status messages are widely used and are often implemented by a JavaScript alert box on the web page. You can call Alert.Show(“Message”) from Asp.Net Codebehind.

On button click, alert box display values in textbox1.
Here is code:

protected void Button1_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + TextBox1.Text.Replace("'","\\'")  + "');</script>");
}

Another way, You can just call javascript from codebehind.

<head runat="server">
    <title>Ashish's Blog</title>
    <script type="text/javascript">
    function callAlert(msg)
    {
        alert(msg);
    }
    </script>
 </head>

Codebehind code:

protected void Button1_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "Message", "callAlert('" + TextBox1.Text.Replace("'","\\'")  + "');",true);
}

If your content page is wrapped in an ASP.NET AJAX UpdatePanel, then you cannot use the ClientScript.RegisterStartupScript to call a JavaScript function during a partial-page postback. Instead, use the ScriptManager.RegisterStartupScript() method.

    protected void Button1_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + TextBox1.Text.Replace("'","\\'")  + "');</script>", false);
     }

Thanks.

8 responses to “Alert(“Message”) box in Asp.Net Codebehind and Call java fuction from codebehind”

  1. hello sir plz helping to send me as email my email is [email protected] a gridview in c# with jquery edit panel thas has dropdownlist control
    the jquery reference not to be in http I like to call my local host files because I don’t have access to the internet always

  2. ‘ASP.net MessageBox
    ‘Add a scriptmanager to the ASP.Net Page

    try:

    Dim sMsg As String = “My Message”

    ScriptManager.RegisterStartupScript(Page, Page.GetType, Guid.NewGuid().ToString(), “alert(‘” & sMsg & “‘)”, True)

Leave a Reply