In this article I will explain how to call JavaScript function from code behind and show JavaScript alert message from code behind using asp.net.
I explained many articles relating to Asp.net, C#.NET, JavaScript, Gridview, JQuery etc. During working with those articles I came across one situation that is show alert message from code behind after completion of my code execution like our JavaScript alert message.
| ScriptManager.RegisterStartupScript(Control control, Type type,string key, string script, bool addScriptTags) |
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Call Javascript functions and show alert message from code behind file in asp.net Page</title> <script type="text/javascript"> function Showalert() { alert('Call JavaScript function from codebehind'); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="btnSubmit" runat="server" Text="Show alert" onclick="btnSubmit_Click" /> <asp:Button ID="btnClick" runat="server" Text="Call JavaScript Function" onclick="btnClick_Click" /> </div> </form> </body> </html> |
using System; using System.Web.UI; |
protected void Page_Load(object sender, EventArgs e) { } //This button click event is used to show alert message directly protected void btnSubmit_Click(object sender, EventArgs e) { ScriptManager.RegisterStartupScript(this,GetType(),"showalert","alert('Only alert Message');",true); } // This button click event is used to show alert message from JavaScript function protected void btnClick_Click(object sender, EventArgs e) { ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true); } |
Imports System.Web.UI Partial Class Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) End Sub 'This button click event is used to show alert message directly Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) ScriptManager.RegisterStartupScript(Me, [GetType](), "showalert", "alert('Only alert Message');", True) End Sub ' This button click event is used to show alert message from JavaScript function Protected Sub btnClick_Click(ByVal sender As Object, ByVal e As EventArgs) ScriptManager.RegisterStartupScript(Me, [GetType](), "displayalertmessage", "Showalert();", True) End Sub End Class |
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|

Subscribe by RSS
Subscribe by Email
7 comments :
This would be of great help. Thanks Suresh !
it's helped lot......
what is the advantage of using javascript in codebehind.
very good thanks
This is great help for me ,
Thank you Suresh !
I have one GridView related question ?
Hi Suresh!, I want to know if the page will get refreshed(i.e if the post back will happen) on clicking of ok in the alert message box.