【问题标题】:How to call confirm dialog from server side如何从服务器端调用确认对话框
【发布时间】:2017-06-20 05:38:29
【问题描述】:

我的页面中有一个按钮,如果条件匹配,它的点击事件会执行某些代码,否则它会要求确认是否继续。我有一个用于确认的 javascript 函数,如下所示。

<script type="text/javascript">

            function getConfirmation(){
               var retVal = confirm("Do you want to continue ?");
               if( retVal == true ){

                  return true;
               }
               else{

                  return false;
               }
            }

      </script>
<asp:Button runat="server" ID="lnkBtn" onClick="lnkBtn_Click" onClientClick="getConfirmation()"></button>

后面的代码是这样的:

   void lnkBtn_Click(Object sender, EventArgs e)
    {
        if (txtMyText.Text!="")
        {
        ///need confirmation here whether to continue.  
        }
        else
        {
        //continue with normal code.    
        }
    }   

现在的问题是即使在不满足条件的情况下单击按钮也会触发 confirm()。我希望 confirm() 仅在满足条件时触发。请帮我做这件事。

谢谢你的期待。

我尝试了这两种解决方案,但似乎我在某处犯了错误。我附上下面的完整代码。请求您帮助我找出我错在哪里以及如何解决它。

<script type="text/javascript">
        function Confirm() {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm("This will completely delete the project. Are you sure?")) {
                confirm_value.value = "Yes";
            }
            else {
                confirm_value.value = "No";
            }
            document.forms[0].appendChild(confirm_value);
        }
    </script>

<asp:Button runat="server" ID="lnkBtn" onClick="lnkBtn_Click" onClientClick="getConfirmation()"></button>

代码背后:

protected void ibExport_Click(object sender, ImageClickEventArgs e)
        {
            string str=gdView.HeaderRow.Cells[8].Text;
            System.Web.UI.WebControls.TextBox txtID = (System.Web.UI.WebControls.TextBox)gdView.Rows[0].Cells[8].FindControl(str);
            if (txtID.Text != "")
            {
                string confirmValue = Request.Form["confirm_value"];
                if (confirmValue == "Yes")
                {
                    MyAlert("Yes clicked");
                }
                else
                {
                    MyAlert("No clicked");
                }
            }
            else
            {
                MyAlert("No Text found.");
            }
        }

使用txtID.Text !=" "点击ibExport时,不显示确认对话框。而是直接弹出“未点击”警报。

【问题讨论】:

    标签: javascript c# asp.net


    【解决方案1】:

    你非常接近,在内联点击处理程序中使用return语句。

    onClientClick="return getConfirmation()"
    

    而不是

    onClientClick="getConfirmation()"
    

    另外,getConfirmation 函数中的if 块是多余的

    function getConfirmation() {
        return confirm("Do you want to continue ?");
    }
    

    【讨论】:

      【解决方案2】:

      删除您的 Javascript。而是在 Backend 中使用此函数进行按钮单击事件。

      void lnkBtn_Click(Object sender, EventArgs e)
          {
              if (txtMyText.Text!="")
              {
                   Response.Write("<script> function getConfirmation()"
                  +" { var retVal = confirm('Do you want to continue ?');"
                  +" if( retVal == true )"
                  +" { return true; } else{ return false;} }"
                  +" </script>");
              }
              else
              {
              //continue with normal code.    
              }
          }   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-20
        • 2020-12-31
        • 2020-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多