【问题标题】:Confirmation message box in webapplicationWeb 应用程序中的确认消息框
【发布时间】:2012-08-05 08:54:50
【问题描述】:

我在 asp.net Web 应用程序中使用以下消息框。现在我想将此消息框转换为确认消息框,并在它为真时执行某些操作,否则意味着拒绝申请。

   ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Are you sure, you want to apply?');</script>", false);

【问题讨论】:

  • 确认你想做什么客户端功能或服务器端调用??

标签: c# asp.net


【解决方案1】:

当用户点击按钮时,尝试对 OnClientClick 事件进行此代码检查

<script type="text/javascript">
      function check()
     {          
        var chk =confirm("Are you sure you are sure?")
        if (chk==true)
        {
            // try you want
        }
        else
        {
            // try you do not want
        }
       return true;
     }

</script>        

 <asp:button id="Button1"
        runat="server"
        Text="Button1"
        OnClientClick="return check();"/>

【讨论】:

    【解决方案2】:
    ScriptManager.RegisterStartupScript(page,this.GetType(), "temp","javascript:calopen();
    ",true);
    
    function calopen()    
    {    
        if (confirm("Are you sure?"+'\n'+"Are you want to delete"))    
        {   
            enter code here    
        }
        else   
        {   
            return false;
        }    
    }
    

    【讨论】:

      【解决方案3】:

      要在 C# 中完全做到这一点,您可以试试这个:

          protected override void OnInit(EventArgs e)
          {  
              AddConfirmationButton();   
              base.OnInit(e);
          }
      
          private void AddConfirmationButton()
          {   
              Button confirmButton = new Button();
              confirmButton.Text = "Action Foo";
              string confirmationMessage = "Are you sure you wish to do action Foo?";
              confirmButton.OnClientClick = "return confirm('" + confirmationMessage + "');";
              confirmButton.Command += confirmButton_Command;
      
              Controls.Add(confirmButton);
      
          }
      
          void confirmationMessage_Command(object sender, CommandEventArgs e)
          {
              DoActionFoo();   //work your magic here.
          }
      

      这会从网页向用户显示“确定/取消”对话框。如果用户单击“确定”,则事件命令中的函数将触发。如果用户单击“取消”,则不会发生任何事情。

      【讨论】:

        【解决方案4】:
        private void showMessage(string msg){
        
                ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('"+ msg +"');</script>", false);
        

        protected void BtnReg_Click(object sender, EventArgs e) {

                OracleHelper.OracleDBOpen();
                object flag = OracleHelper.OracleExecuteScalar("your select Query ");
                if (flag == null)
                {
                              showMessage("Failed !!! ");
        
                }
                else
                {
                    string reg = String.Format("your Insert Query ");
        
                    showMessage("successfuly");
                    OracleHelper.OracleExecuteNonQuery(reg);
        
                }                
                OracleHelper.OracleDBClose();
            }
        }
        

        【讨论】:

          【解决方案5】:

          试试这个

          将此添加到您的 cs 文件中以显示 confirm 而不是 alert

          string confirm = 
          "if(confirm('Are you surely want to do this ??')) __doPostBack('', 'confirmed');";
          ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", confirm, true);
          

          在同一页面上添加此项以检查用户何时来自该确认框

          protected void Page_Load(object sender, EventArgs e)
          {
              string parameter = Request["__EVENTARGUMENT"];
              if (string.Equals("confirmed", 
                                  parameter, 
                                  StringComparison.InvariantCultureIgnoreCase))
              {
                  // Call your server side method here
              }
          }
          

          为此,我使用了__doPostBack,您可以从here 了解更多信息。 希望对你有帮助

          【讨论】:

            【解决方案6】:

            与确认框等效的javascript 是confirm() 方法。此方法根据用户的“确定”或“取消”按钮响应返回真或假值。

            用法:

            var confirmed = confirm('Are you sure?','Are you sure you want to delete this item?');
            
            if(confirmed){
              //do something
            } else {
              //do something else
            }
            

            【讨论】:

              【解决方案7】:

              在下面注册脚本而不是警报

              <script type="text/javascript">
              var r=confirm("Are you sure you are sure?")
              if (r==true)
              {
                //You pressed OK!
              }
              else
              {
                //You pressed Cancel!
              }
              </script>
              

              【讨论】:

                【解决方案8】:

                我认为你的做法是错误的。您应该在发回之前显示此确认信息,然后仅在他们选择“应用”时才发回。

                使用 ASP.NET Web 控件,Button 控件有一个OnClientClick 属性,可用于在 Http POST 之前调用 javascript:

                你可以这样做:

                <asp:button id="btn"
                            runat="server"
                            Text="Apply"
                            OnClientClick="return confirm('Are you sure you wish to apply?');"
                            OnClick="btn_Click" />
                

                【讨论】:

                • 这对我有用.. 那么我如何在确认消息中使用换行符,例如“return confirm('Are you sure
                  you want to apply?');”跨度>
                • 我相信你用\n而不是&lt;br /&gt;
                • 我这样使用'Are you +'\n'+ 你想申请吗?它不工作
                • 只需使用Are you \n you wish to apply?。不需要断线
                猜你喜欢
                • 2015-08-19
                • 2012-03-31
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-03-05
                相关资源
                最近更新 更多