Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.net show alert message from code behind using javascript

Apr 6, 2012
Introduction:

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.
Description:
  
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.

To display JavaScript alert message from code behind we can do by using ScriptManager.RegisterStartupScript() method. This method will allow us to show alert message direclty from code behind or we can call javascript function from code behind in asp.net.

Declaration of ScriptManager.RegisterStartupScript() metod:

ScriptManager.RegisterStartupScript(Control control, Type type,string key, string script,
bool addScriptTags)
Here I will explain how to show JavaScript alert message in code behind and how to call JavaScript function from code behind in asp.net. For that first create new website and write following code in aspx page


<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>
Now add following namespaces in codebehind file

using System;
using System.Web.UI;
After that write the following code in code behind

C# code


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);
}
VB.NET

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
Demo


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 RSS subscribe by email Subscribe by Email

15 comments :

Anonymous said...

This would be of great help. Thanks Suresh !

varaprasad said...

it's helped lot......

Anonymous said...

what is the advantage of using javascript in codebehind.

Anonymous said...

very good thanks

Rumit Gupta said...

This is great help for me ,
Thank you Suresh !

I have one GridView related question ?

Anusha Polavarapu said...
This comment has been removed by the author.
Anusha Polavarapu said...

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.

Anonymous said...

u r great sir

Anonymous said...

sir i waana become like u, ur article helps a lot, i m really thanks u from bottom of my heart

Anonymous said...

i have a login page in which i have user id and password in asp.net C#.sir i want to give a remember me checkbox .when user click on remember me checked box then user name and password will saved in both textbox whenever user try login next time.plz help me sir

Anonymous said...

Thanks a lot!!!

Anonymous said...

great work

P.V.Anil Kumar said...

thank you

sai Sucess said...

need code in .net for popup msg for birthdays in gmail

Sambit Kumar Mishra said...

This Code is not Working in my Command Button Which is in Content Page Of A Master Page.

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.