我有以下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" />
<asp:Button ID="BtnNo" runat="server" Text="Cancel" onclick="BtnNo_Click" />
</div>
当用户点击提交时,代码开始执行,如果以下函数为true,我想在继续执行代码之前显示确认对话框:
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做到这一点?
答案 0 :(得分: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;
}
答案 1 :(得分:0)
你可以通过几种方式做到。
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;
}