AspnetO

We code, that works!

  • Home
  • Asp.net
  • MVC
  • Interview Questions

Show Confirm Message Box from Code-behind in Asp.net

By: Mayank Modi | Folls In: Asp.net | Last Updated: Nov 28, 2014

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

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

We can use a confirm message box if we want the user to verify or accept something before taking any action such as want to ask before deleting records, agree with terms and conditions and so on. For example, insert update delete on gridview data and gridview inline insert update delete are the two posts where I’d implemented confirm message box to delete records using javascript practically.

We can’t 100% show confirmation box from code-behind (without using any third-party controls or jQuery), for that we need the mixture of server-side and client-side support. So, first we need to add the following javascript function in .aspx page in head section, like this:

Confirm Message Box JavaScript Function

<script type="text/javascript">
       function CallConfirmBox() {
               if (confirm("Confirm Proceed Further?")) {
                       //OK – Do your stuff or call any callback method here..
                       alert("You pressed OK!");
               } else {
                       //CANCEL – Do your stuff or call any callback method here..
                       alert("You pressed Cancel!");
               }
       }
</script>

To call confirm message box function from server-side, we need to define OnClick=”btnCall_Click” event of the asp.net button, like this:

<asp:Button ID="btnCall" runat="server" Text="Click Me!" OnClick="btnCall_Click" />

Now we can call that method from code-behind as follows.

For C#:

protected void btnCall_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "CallConfirmBox", "CallConfirmBox();", true);
}

For Vb.net:

Protected Sub btnCall_Click(ByVal sender As Object, ByVal e As EventArgs)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "CallConfirmBox", "CallConfirmBox();", True)
End Sub

Confirm Message Box Example – [.aspx]

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
       <title>Call Confirm Message Box from code-behin in Asp.net</title>
       <script type="text/javascript">
               function CallConfirmBox() {
                       if (confirm("Confirm Proceed Further?")) {
                               //OK – Do your stuff or call any callback method here..
                               alert("You pressed OK!");
                       } else {
                               //CANCEL – Do your stuff or call any callback method here..
                               alert("You pressed Cancel!");
                       }
               }
       </script>
</head>
<body>
       <form id="form1" runat="server">
       <h4>
               Call Confirm Message Box from code-behin in Asp.net</h4>
       <div>
               <asp:Button ID="btnCall" runat="server" Text="Click Me!" OnClick="btnCall_Click" />
       </div>
       </form>
</body>
</html>

Example Result

How to Show Confirm Message Box From Code-behind in Asp.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.

Comments

  1. Jungeun.K says

    Sep 19, 2018 at 12:18 AM

    It helps a lot. Tks.

    Reply
  2. Mr.T says

    Mar 11, 2015 at 8:54 AM

    Your code is great!

    I remember that when I was a beginner in asp.net I searched this above code. Thanks for your sharing.

    Reply

Share Your Comments & Feedback Cancel reply

Note: To post any code in comment, use <pre>your code</pre>

Social Connections

  • 0 Fans
  • 3,222 Followers
  • 21 Followers
  • 52 Followers
  • 1,559 Subscribers

Top Posts

  • CSS3 Transition: fadeIn and fadeOut like Effects to Hide Show Elements
  • Top 10 OOPS Concepts In C# .NET With Examples
  • Parser Error While Deploying Website to Server in Asp.net
  • Asp.net TextBox: How to Get Set TextBox Value or Text in jQuery
  • How to Print Asp.net GridView Data on Button Click using Javascript?

Find 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

Hot on AspnetO

Icon
Gridview Insert Update Delete Example in Asp.net, C#, Vb.net 7245 downloads 39.76 KB
Download This Example
Icon
Gridview Insert Update Delete Example in Asp.net, C#, Vb.net 6211 downloads 39.76 KB
Download This Example
Icon
Gridview Insert Update Delete Example in Asp.net, C#, Vb.net 6207 downloads 39.76 KB
Download This Example
Icon
Export gridview all rows or selected rows to word, excel, text, pdf examples 3056 downloads 1.01 MB
Download This Example
Icon
Export gridview all rows or selected rows to word, excel, text, pdf examples 2993 downloads 1.01 MB
Download This Example

Copyright © 2014 - 2019 · All Rights Reserved.replica cartier watches

About | Copyrights | Privacy | Terms | Contact | Advertise | Sitemap
Previous Confirm Message Box in JavaScript Example For Asp.net
Next Best 100+ Frequently Asked Interview Questions in .Net SQL