<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to show alert message from code-behin in Asp.net</title>
<script type="text/javascript">
function alertMessage() {
alert('JavaScript Function Called!');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h4>
Show alert message from code-behin in Asp.net</h4>
<div>
<asp:Button ID="btnServerSideMethod" runat="server" Text="Server-side(Method1)"
OnClick="btnServerSideMethod_Click" />
</div>
</form>
</body>
</html>We need to use ScriptManager.RegisterStartupScript() method to achieve this. Here is the syntax for this method:
ScriptManager.RegisterStartupScript(Control control, Type type,string key, string script,
bool addScriptTags);Now define that event in code-behind as shown below.
Show Alert Message From Code-behind In C# - [.cs]
Following is the C# code to display or show alert message:
//This method is used when you defined javascript function and want to call it
//So call pre-defined method this way
protected void btnServerSideMethod_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", "alertMessage();", true);
} 