【问题标题】:JavaScript alert with yes no button带有是否按钮的 JavaScript 警报
【发布时间】:2014-09-10 12:15:51
【问题描述】:

我想用 yes no 按钮显示警报。

这是我的 C# 代码:

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 = "";
        }
    }
}

此代码运行良好,但我想使用 JavaScript 警报和 yes no 按钮来执行此操作。 我正在使用 Visual Studio 2010 asp.net c#。

【问题讨论】:

  • 您在寻找confirm吗?
  • @MarcellFülöp 确认(是,否)
  • 嗯,你在 JS 中有 confirm(),并且按钮标签取决于浏览器/区域设置。

标签: c# javascript asp.net


【解决方案1】:
  <asp:Button ID="btn" runat="server" Text="High"
  OnClick="Button1_Click" OnClientClick="return AalertFunction();" />

Java 脚本:

     <script>     
        function AalertFunction() {
            if (confirm('Are you sure you want to save this thing into the database?')) {
                return;
            } else {
                return false;
            }
        }
    </script>

【讨论】:

  • 为什么会显示“阻止此页面创建其他诊断日志”消息。
  • 我认为您无法替换它们。如果需要,您应该制作自定义确认框。而关于“附加对话框”,它是一个浏览器功能,当您一遍又一遍地出现警告框时会显示它。
【解决方案2】:

如果你使用 Twitter Bootstrap 作为你的界面框架,

我会推荐Bootbox.js,因为它已经简化了确认功能:

取自网站:

bootbox.confirm("Are you sure?", function(result) {
  Example.show("Confirm result: "+result);
}); 

【讨论】:

    【解决方案3】:

    使用 jQuery 你可以创建一个自定义对话框

    $(function() {
        $( "#dialog-confirm" ).dialog({
          height:140,
          modal: true,
          buttons: {
            "Yes": function() {
              //Do your yes function
            },
            "No": function() {
              //Do your no function
            }
          }
        });
      });
    

    【讨论】:

    • 这个答案显示了承诺,但它确实可以使用一些关于如何使用它的信息。您如何指定提示的文本,如您问用户的问题:“您确定要这样做吗?”。而且,您是否只是更改文本“是”和“否”来更改按钮上的标签?你可以有两个以上的按钮吗? (如果是,如何?)。你如何“调用”这个(你如何触发对话框打开)?最后,是否有任何依赖项(除了 jQuery)?
    猜你喜欢
    • 1970-01-01
    • 2011-01-04
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 2011-06-24
    相关资源
    最近更新 更多