Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now

Asp.net show alert message from code behind using javascript


In this article we will learn how to call JavaScript function and show JavaScript alert message from code behind using asp.net.
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>

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);
}




Share this

Related Posts

Previous
Next Post »

Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now