1

I want to show alert with yes no button.

here is my C# code :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Collections;
using System.Windows.Forms;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DialogResult dialogResult = MessageBox.Show("Sure", "Some Title", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            OleDbConnection con = new OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;data source= C:/Users/xyz/Documents/Visual Studio 2010/WebSites/accesspassword/stud.accdb;Jet OLEDB:Database Password=12345;");
            con.Open();

            OleDbCommand com = new OleDbCommand("insert into Table1 values(@Roll,@f_name)", con);
            OleDbParameter obj1 = new OleDbParameter("@Roll", DbType.StringFixedLength);
            obj1.Value = TextBox1.Text;
            com.Parameters.Add(obj1);

            OleDbParameter obj2 = new OleDbParameter("@f_name", DbType.StringFixedLength);
            obj2.Value = TextBox2.Text;
            com.Parameters.Add(obj2);

            com.ExecuteNonQuery();
            TextBox1.Text = "";
            TextBox2.Text = "";
            con.Close();
        }
        else if (dialogResult == DialogResult.No)
        {
            TextBox2.Text = "";
        }
    }
}

This code is running fine but i want to do this using JavaScript alert with yes no button. I am using Visual Studio 2010 asp.net c#.

user3834541
  • 161
  • 1
  • 7
  • 27

3 Answers3

5
  <asp:Button ID="btn" runat="server" Text="High"
  OnClick="Button1_Click" OnClientClick="return AalertFunction();" />

Java Script:

     <script>     
        function AalertFunction() {
            if (confirm('Are you sure you want to save this thing into the database?')) {
                return;
            } else {
                return false;
            }
        }
    </script>
Vivekh
  • 4,141
  • 11
  • 57
  • 102
  • why this show me "Prevent this page from creating additional diaglogs" message. – user3834541 Jul 19 '14 at 14:11
  • I dont think you can replace them. If you need to you should make your Custom Confirm Box. And Regarding "additional Dialog's" It's a browser feature that will be shown when you have alert boxes over and over again. – Vivekh Jul 21 '14 at 12:57
0

Using jQuery you could create a custom dialog

$(function() {
    $( "#dialog-confirm" ).dialog({
      height:140,
      modal: true,
      buttons: {
        "Yes": function() {
          //Do your yes function
        },
        "No": function() {
          //Do your no function
        }
      }
    });
  });
Josh
  • 119
  • 2
  • This answer shows promise, but it could really use some information about how to use this. How do you specify the text of the prompt, as in the question you are asking the user: "Are you sure you want to do that?". And, do you just change the text "Yes" and "No" to change the labels on the buttons? Can you have more than 2 buttons? (If Yes, how?). How do you "invoke" this (how do you trigger the dialog to open)? And finally, are there any dependencies (other than jQuery)? – Kevin Fegan Feb 13 '21 at 18:58
0

If you are using Twitter Bootstrap as your interface framework,

I would recommend Bootbox.js as it already has the confirm functionality simplified:

Taken from the site:

bootbox.confirm("Are you sure?", function(result) {
  Example.show("Confirm result: "+result);
}); 
Kyle Emmanuel
  • 2,193
  • 1
  • 15
  • 22