AspnetO

We code, that works!

  • Home
  • Asp.net
  • MVC
  • Interview Questions
You are here: Home / Asp.net / Show Alert Message Box from Code-behind in Asp.net C# Vb

Show Alert Message Box from Code-behind in Asp.net C# Vb

By: Mayank Modi | Falls In: Asp.net, C#, VB | Last Updated: Oct 10, 2020

In my previous tutorials, I’d explained how to show alert message from client-side, how to call javascript function from code-behind, confirm message box example using javascript and more cracking tutorials on Asp.net, JavaScript, jQuery here.

Now here in this tutorial, I’ll explain how to show alert message box from server-side or code-behind in asp.net using c# or vb.net with example code.

Show Alert Message from Code-behind – [.aspx]

Following is the complete HTML Markup code that I used in my .aspx page for this demonstration:

<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=”btnClientSide” runat=”server” OnClientClick=”alertMessage()”
Text=”Client-side” />
<asp:Button ID=”btnServerSideMethod1″ runat=”server” Text=”Server-side(Method1)”
OnClick=”btnServerSideMethod1_Click” />
<asp:Button ID=”btnServerSideMethod2″ runat=”server” Text=”Server-side(Method2)”
OnClick=”btnServerSideMethod2_Click” />
</div>
</form>
</body>
</html>

You can see, i’d added OnClientClick=”alertMessage()” when calling function from client-side. In case of calling javascript function from server-side or code-behind, we need OnClick event as OnClick=”btnServerSideMethod1_Click” and OnClick=”btnServerSideMethod2_Click”.

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:

//This method is used when you defined javascript function and want to call it
//So call pre-defined method this way
protected void btnServerSideMethod1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), “alertMessage”, “alertMessage();”, true);
}//This method is used when you don’t want to define javascript method client-side
//So call directly this way
protected void btnServerSideMethod2_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), “alertMessage”,
“alert(‘Called from code-behind directly!’);”, true);
}

OR

If you are dealing with asp.net UpdatePanel and UpdateProgress, use the following code:

//Following statement is used to call pre-defined javascript function
protected void btnServerSide_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(myUpdatePanelID, myUpdatePanelID.GetType(), “myAlert”,
“alert(‘Called from code-behind directly!’);”, true);
}

Show Alert Message From Code-behind In Vb.net – [.vb]

Likewise, following is the Vb.net 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 Sub btnServerSideMethod1_Click(ByVal sender As Object, ByVal e As EventArgs)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), “alertMessage”, “alertMessage();”, True)
End Sub‘This method is used when you don’t want to define javascript method client-side
‘So call directly this way
Protected Sub btnServerSideMethod2_Click(ByVal sender As Object, ByVal e As EventArgs)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), “alertMessage”,
“alert(‘Called from code-behind directly!’);”, True)
End Sub

OR

If you are dealing with asp.net UpdatePanel and UpdateProgress, use the following code:

‘Following statement is used to call pre-defined javascript function
Protected Sub btnServerSide_Click(ByVal sender As Object, ByVal e As EventArgs)
ScriptManager.RegisterStartupScript(myUpdatePanelID, myUpdatePanelID.GetType(),
“myAlert”, “alert(‘Called from code-behind directly!’);”, True)
End Sub

Example Result

How to show alert message box from code-behind in asp.net c# vb.net?

Signup Today And Get Latest Tutorials For Free!

Subscribe to us and get free latest tutorials notifications whenever we publish a new contents.

<

About Mayank Modi

Mayank is a web developer and designer who specializes in back-end as well as front-end development. He's a Founder & Chief Editor of AspnetO. If you'd like to connect with him, follow him on Twitter as @immayankmodi.

Leave a Reply Cancel reply

Search Your Topic



Social Connections

  • 1,438 Fans
  • 3,095 Followers
  • 51 Followers
  • 1,559 Subscribers

Get Latest Tutorials For Free



Top Posts

  • Asp.net Menu Control Example
  • CSS3 Transition: fadeIn and fadeOut like Effects to Hide Show Elements
  • Show Confirm Message Box from Code-behind in Asp.net
  • Asp.net TextBox: How to Get Set TextBox Value or Text in jQuery
  • Pass Multiple Parameters in Asp.net QueryString Example

Contribute to AspnetO

If you want to contribute your unique blog articles or tutorials (Free / Paid) to AspnetO in any languages, you're most welcome. Just send me your previous articles, and topics on which you are interested to post an tutorial. Contact us at email listed in contact us page. Selected candidates will be contacted.

Search by Tags

Ado.net Ajax appSettings Asp.net C# CheckBox CheckBoxList ConnectionStrings Control CSS CSS3 Difference Download DropDownList Export Facebook fadeIn fadeOut fadeTo fadeToggle File File Extension FileUpload Function GridView IIS Interview Questions JavaScript jQuery MVC OOP RadioButtonList RDP Repeater Send Mail Solutions Split SQL Stored Procedure TextBox Upload Validation VB Web.config Web Hosting

The Man Behind AspnetO

Mayank Modi

Hi there,

Myself Mayank Modi, a Full Stack Developer (.NET Stack) and a blogger from Surat, India.

I'm welcoming you to my blog - AspnetO, a programmers community blog where we code, that works!

I started AspnetO as a hobby and now we're growing day by day. We're now having 5000+ programmers that get benefits and learn new things about website design and development under our community blog.

Here at AspnetO, I write about Beginners to Advance level of tutorials on programming languages like Asp.net using C# and Vb.net, MVC, SQL Server, JavaScript, jQuery etc. In sort, all about .NET Framework and website development stuff and sometimes sharing tips and tricks that can help you to grow up your programming skills.

You can get more details about me and my blog at About us page.

Subscribe To Newsletter

Enter your email address to subscribe to this blog and receive notifications of new posts right to your inbox

Join 1000+ other subscribers

<

Recent Posts

  • Main Difference between SessionState and ViewState in Asp.net
  • How to Get appSettings Value from Web.config File?
  • How to Get ConnectionString from Web.config in Asp.net?
  • Difference between appSettings and connectionStrings in Web.config
  • Get Folder Files List and Export to CSV in .NET
  • Get Files List From Directory Recursively in C# Vb.net
  • Get Hash Value From Current Page URL In jQuery
  • Handle Multiple Submit Buttons in Single MVC Form

Copyright © 2014 - 2021 · All Rights Reserved.

About | Copyrights | Privacy | Terms | Contact | Advertise | Sitemap
Previous Show Alert Message in JavaScript or jQuery
Next Call JavaScript Function from Code-behind in Asp.net C# Vb