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.