-5

我有以下 div (通常隐藏)

<div id="confirmDialog" class="window" style="background-color:#f2f4f7; border:1px solid #d9a0e2; text-align:center; height:100px; position:fixed;  padding:10px;  top:50%  " runat="server" visible="false">
<br /> You already have a leave request on the chosen date. Are you sure you want to submit this request?<br /><br />
<asp:Button ID="BtnConfirm" runat="server" Text="Yes"  Width="60px" />&nbsp;
<asp:Button ID="BtnNo" runat="server" Text="Cancel" onclick="BtnNo_Click" />
</div>

当用户单击提交时,代码开始执行,如果以下函数为真,我想在继续执行代码之前显示一个确认对话框:

protected void BtnAdd_Click(object sender, EventArgs e)
{
    //some validations
    if (new LeaveLogic().GetEmployeeLeaveRequestByDate(username, Convert.ToDateTime(TxtBoxDate.Text)) > 0)
    {
           confirmDialog.Visible = true;
           /if BtnConfirm is click continue to execute code 
           //else stop

我怎样才能通过 asp.net/jQuery 做到这一点?

4

2 回答 2

1

您必须将代码分成两个不同的部分:一个执行并在完成后弹出一个确认对话框,第二个部分是您提交表单以执行其余部分。你不能一次性做到这一点,因为你不能让服务器端代码执行,在客户端弹出一个确认对话框,然后在服务器端继续。

你要做的是(在伪代码中)

button1_Click()
{
  Execute_logic;
  use scriptmanager to trigger a JavaScript function that displays the confirmation dialog;
}

JavaScript 函数应该:

function askConfirm()
{

  if(confirm('want to continue?'))
     submit_the_form to execute second part of the process();
  else 
     return false;
}

再次服务器端代码:

//This is the method that should execute after the JavaScript function submits the form
Handler_ForSecondPartOfTheRequest()
{
  execute second part of the logic;
}
于 2013-07-30T09:20:55.653 回答
0

你可以通过几种方式来做。

  1. 通过Jquery拦截点击事件
  2. 在您的代码中添加其他脚本。

protected void Button1_Click(object sender, EventArgs e)
    {
        ClientScriptManager CSM = Page.ClientScript;
        if (!ReturnValue())
        {
            string strconfirm = "<script>if(!window.confirm('Are you sure?')){window.location.href='Default.aspx'}</script>";
            CSM.RegisterClientScriptBlock(this.GetType(), "Confirm", strconfirm, false);
        }
    }
    bool ReturnValue()
    {
        return false;
    }
于 2013-07-30T09:09:12.510 回答