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

Show Asp.net Alert Message from Server Side in C#, VB.NET

Apr 8, 2013
Introduction:

In this article I will explain how to show alert message from server side using asp.net in C#, VB.NET.

Description:
  
In previous articles I explained Call JavaScript function from code behind in asp.net, Call Server side function from JavaScript, Call Page methods from JSON in asp.net many articles relating to Asp.net, C#.NET, JavaScript, Gridview, JQuery etc. Now I will explain how to show alert message from server side using asp.net in C#, VB.NET.

To display JavaScript alert message from server side we need to use 

ScriptManager.RegisterStartupScript() method. This method will allow us to show alert message directly from server side 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

8 comments :

Anonymous said...

chandru said...

how to implement Google translate in website

Lalitha said...

Please Give a example for mobile application using c#

deviuthandan said...

Give the example of chat application using C#

Anonymous said...

very simple to understand ur explanation with demo,Excellent.

Anonymous said...

hi sir,

how can we use scriptmanger.registerstartup in c# class file...
please help me.

Unknown said...

when iam using ScriptManager.RegisterStartupScript the dropdown in the page is losing it's CSS..any solution for it

Unknown said...
This comment has been removed by the author.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 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.