[英]Clicking on cancel on Javascript Confirm box does not stop the grid from refreshing
如何在javascript确认框上点击取消按钮停止呈现.cs
代码页面?
我有一个按钮点击事件,在某些IF
条件下,我要求用户确认。 现在,当用户单击“确定”按钮时,我必须在网格中绑定新数据。 但如果用户单击取消按钮,则不应使用新数据刷新网格。
问题:点击JavaScript确认框中的取消按钮,如何停止执行以下代码,或者如何从JavaScript返回/中断?
有人可以帮我解决这个问题吗? 提前致谢。
标记
<asp:Button ID="btnCopy" runat="server" Text="Copy" Enabled="false" OnClick="btnCopy_Click" ValidationGroup="Copy" />
码
public void btnCopy_Click(object sender, EventArgs e) {
if (Convert.ToInt32(hidId.Value) > 0) {
string scriptString = "<script language='JavaScript'> ";
scriptString += "if (confirm('Are you sure to proceed?') == false) {return} ";
scriptString += "</script>";
Page.RegisterStartupScript("Confirm", scriptString);
BindDataGrid();
}
}
您在同一事件中混合服务器和客户端代码,它必须分开:
脚本:
function Confirm()
{
var record=confirm('Are you sure to proceed??');
if(record == 1)
{
return true;
}
else if(record == 0)
{
return false;
}
}
标记:
<asp:Button ID="btnCopy" runat="server" Text="Copy" Enabled="false"
OnClick="btnCopy_Click" OnClientClick='return Confirm();' ValidationGroup="Copy" />
代码隐藏:
public void btnCopy_Click(object sender, EventArgs e) {
if (Convert.ToInt32(hidId.Value) > 0) {
BindDataGrid();
}
}
您需要捕获响应,例如:
if (window.confirm)
{
//handle the OK
}
else
{
//handle the cancel
}
请显示代码,但是如果没有看到您的代码我会猜你没有回复响应?
<input type="button" onclick=" return confirm('is this right?'); ">
您只需从代码return
或exit(0)
。
function fillGrid()
{
if( confirm("....") )
{
//populate your grid
}
else
{
return;
}
//do anything after the grid is populated
//and exit the function
}
您在按钮单击的代码隐藏中显示确认对话框,不会停止代码流。
在进行回发之前,您需要显示确认对话框
<asp:Button ID="btnCopy" runat="server" Text="Copy" Enabled="true" OnClientClick="return confirm('sure?');" ValidationGroup="Copy" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.