Asp.Net按钮onclientclick问题
我似乎有一个奇怪的问题,我不知道如何解决。 我有一个像这样的按钮:
<asp:Button ID="btnSave" runat="server" ClientIDMode="Static" Text="Save" OnClientClick="return ConfirmSave();" OnClick="btnSave_Click" />
如果我像下面这样编写客户端函数,它会按预期工作:
function ConfirmSave()
{
return confirm('Confirm?');
}
但我应该在函数内部检查确认结果,如下所示:
function ConfirmSave()
{
if (Page_ClientValidate('validationGroup'))
{
var conf = confirm("Confirm?");
if (conf)
{
$('#btnSave').attr('disabled', 'disabled');
}
return conf;
}
else
return false;
}
这样做,页面回发但服务器单击事件不着火。
有人可以帮忙吗?谢谢
I have a strange problem, it seems, that I don't know how to solve.
I have a button like this:
<asp:Button ID="btnSave" runat="server" ClientIDMode="Static" Text="Save" OnClientClick="return ConfirmSave();" OnClick="btnSave_Click" />
If I write my client function like the following, it works as expected:
function ConfirmSave()
{
return confirm('Confirm?');
}
But I should check, inside the function, for the confirm result, like this:
function ConfirmSave()
{
if (Page_ClientValidate('validationGroup'))
{
var conf = confirm("Confirm?");
if (conf)
{
$('#btnSave').attr('disabled', 'disabled');
}
return conf;
}
else
return false;
}
Doing this, the page postback but the server click event doesn't fire.
Anyone could help? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在代码完成并提交发生后立即使用超时来禁用按钮。
use a timeout to disable the button immediately after your code completes and the submit happens.
是的,它会触发,因为按钮是使用提交类型输入的。
您可能有其他错误,可能是页面中按钮 ID 的重复(因为您已设置它)静态模式)。
检查您是否在其他地方再次给出了btnSave,或者检查是否有任何 JavaScript 错误。
(页面渲染后,在浏览器上查看页面的源代码并搜索 btnSave id(如果存在多次)
Yes its fires because the Button is input with submit type.
You probably have some other error, maybe a duplicate of your button id, in the page (because you have set it on static mode).
Check if you have give the btnSave again somewhere else, or check also if you have any JavaScript error.
(After your page is render, on your browser see the source code of your page and search for the btnSave id if exist more than one time)
如果您的代码中有一个名为 btnSave_Click() 的方法,那么当您回发时,它将首先落入您的 Page_load 方法,然后落入该方法。如果您使用的是 VB,那么您将需要一个在其后带有
Handles btnSave_Click()
的方法。If you have a method called btnSave_Click() in your code behind then when you post back it will fall into your Page_load method first and then fall into that method. If you are using VB then you will need a method with
Handles btnSave_Click()
after it.