Asp.net show alert message from code behind using javascript

Asp.net show alert message from code behind using javascript

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

<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

 

 

About these ads

About Vijay Meghwal

i just comin... Live project Training Asp.net c# Ajax Javascript ,Silverlight,jquery,WCF,WPF,MVC,XML
This entry was posted in JavaScript. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s