如何使用两个确认对话框?

时间:2015-09-19 07:49:31

标签: javascript c# asp.net

我有一个链接按钮,在点击时显示确认对话框,然后确认我必须在数据库中搜索对话框,如果找到值,则再次显示确认对话框,然后再确认对话框做一些搜索任务。为了更好地理解,我附上了流程图。

enter image description here

Aspx Page

 <script language="javascript" type="text/javascript">
        function confirm_payslip() {
                            var res = confirm("Are you sure to upload current month payslip data");
                            if (res) {
                                //return true;
                                document.getElementById("HiddenField1").value = "1";
                               var myName = <%=this.firstName%>;
                               if(myName==1)
                               {
                               var result = confirm("Payslip Already Uploaded. Do you want to replace it?");
                               if(result)
                               {
                               document.getElementById("HiddenField1").value = "2";
                               }
                               else
                               {
                                document.getElementById("HiddenField1").value = "0";
                                return false;
                               }
                               }
                            }
                            else {

                                document.getElementById("HiddenField1").value = "0";
                                return false;
                            }
                            return true;                         //document.getElementById("ctl00_ContentPlaceHolder1_div_PayTax").className = "loading"; 
                        }
        </script>
<asp:LinkButton ID="lbtn_payslip" runat="server" OnClientClick="confirm_payslip();" OnClick="lbtn_payslip_Click">Upload PaySlip Data</asp:LinkButton>
<asp:HiddenField ID="HiddenField1" runat="server" />

CS页

public int firstName = 0;
    protected void lbtn_payslip_Click(object sender, EventArgs e)
        {
            if (HiddenField1.Value == "1")
            {
                qry = "select count(*) from tbl_payslip_prn_t";
                dt = con.Execute(qry);
                if (dt.Rows.Count > 0)
                {
                    firstName = 1;
                }
                else
                {
                     firstName = 0;
                     //Do Some task
                }
            }
            else if (HiddenField1.Value == "2")
            {
                //Do some task
            }
            else
            {
                firstName = 0;
                return;
            }
    }

问题是第二个对话框以偶数运行,如第二次点击,第四次点击等。那么如何实现上述任务呢?

3 个答案:

答案 0 :(得分:1)

在您的aspx上添加一个隐藏的asp:Button并附加一个事件处理程序,并在确认按钮上单击是时编写您想要执行的代码。

ClientScript.RegisterStartupScript(typeof(Page), "MyScript", "if(confirm(\"Payslip Already Uploaded. Do you want to replace it?\")){ document.getElementById('Button1').click(); }", true);

答案 1 :(得分:0)

这个演示可能有帮助吗? http://jsfiddle.net/r8vf9pyw/

function confirm_payslip() {
  var myName = document.getElementById('myName').value;
  var input = document.getElementById('HiddenField1');
  var method;

  var res = confirm('Are you sure to upload current month payslip data');
  if (res) {
    if(myName==1) {
      var result = confirm("Payslip Already Uploaded. Do you want to replace it?");
      if(result) {
        input.value = "upload and replace it";
        method = 'upload';
      } else {
        input.value = "file exists, do not upload";
        method = 'abort';
      }
    } else {
      input.value = "upload new file";
      method = 'upload';
    }
  } else {
    input.value = 'upload aborted';
    method = 'abort';
  }

  if ('upload' === method) {
    document.getElementById('output').innerHTML = 'Uploading...';
    return true;
  } else {
    document.getElementById('output').innerHTML = 'Failed. Upload aborted';
    return false;
  }
}

答案 2 :(得分:0)

我的方法略有不同,但它会让你达到你想要的效果。

cs Page:

protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (HiddenField1.Value.Trim() == "0")
        {
            return;
        }
        qry = "select count(*) from tbl_payslip_prn_t";
        dt = con.Execute(qry);
        if (dt.Rows.Count > 0)
        {
            ClientScript.RegisterStartupScript(typeof(Page), "exampleScript", "if(confirm(\"Payslip Already Uploaded. Do you want to replace it?\")){ document.getElementById('Button1').click(); }", true);
        }
        else
        {
            //Do Some task
        }
    }  
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Do Some Task    
    }

aspx页面:

<script type="text/javascript">
        function confirm_payslip() {
            var res = confirm("Are you sure to upload current month payslip data");
            if (res) {
                //return true;
                document.getElementById("HiddenField1").value = "1";
            }
            else {

                document.getElementById("HiddenField1").value = "0";
                return;
            }
        }
    </script>
 <div>
        <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="confirm_payslip();" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>

    </div>
    <div style="display:none">
     <asp:Button ID="Button1" runat="server" Text="Button" 
            onclick="Button1_Click" />
     <asp:HiddenField ID="HiddenField1" runat="server" />
    </div>