如何在执行服务器代码之前禁用ASP.net按钮控件 [英] How to disable ASP.net button control before execute server code

查看:59
本文介绍了如何在执行服务器代码之前禁用ASP.net按钮控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在ASP.net页面中有这个ASP.net按钮:

Hi everyone,

I have this ASP.net button in ASP.net page:

<asp:Button ID="btnVerifyEmail" runat="server" Text="Verify Email" OnClientClick="return CheckForRequiredFields()" OnClick="btnVerifyEmail_Click" />



当我按下按钮时,它首先执行Javascript方法,如果返回false,将不会转到服务器,否则将转到服务器执行服务器代码,

我想在执行服务器代码之前使Button处于禁用状态,以防止用户再次按下它,因此我将此代码添加到了页面的窗体加载中:



When i press button, it firstly execute Javascript method that if it return false will not go to server else go to server to execute server code,

I want to make the Button disabled before executing server code to prevent User from press on it again, so i added this code to the form load of the page:

btnVerifyEmail.Attributes.Add("onclick", "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(btnVerifyEmail, "").ToString());



仅当我删除OnClientClick ="return CheckForRequiredFields()",
时,此代码才能正常工作
但是如果我使用Javascript方法,则该按钮不会被禁用,用户可以越来越多地按下它

那我该怎么办



this code work fine only if i remove the OnClientClick="return CheckForRequiredFields()",

but if i used Javascript method the button not be disabled and user can press it more and more

so what can i do
any idea?

推荐答案

很明显,Page.ClientScript.GetPostBackEventReference无法正常工作,因为在单击按钮并发生回发之后,它在服务器上. br/>
Obviously Page.ClientScript.GetPostBackEventReference won''t work because its on the server, after the button has been clicked and the postback has occcured.

<asp:Button id=''btnVerifyEmail'' runat=''server'' Text=''Verify Email'' onclientclick=''return CheckForRequiredFields(this)''/>

function CheckForRequiredFields(btn)
{
  btn.disabled = isValid();
  return !btn.disabled;
}



无论验证函数是什么,如果它返回true,它将禁用属性设置为true并返回false,这将不允许发生回发.



Whatever you validation function is if it returns true it will set the disabled property to true and return false which will not allow the postback to occur.


如果您返回 true 然后只需添加代码以将其禁用,如下所示:
If you are returning true then just add a code to disable it, something like this :
document.getelementbyid(clientId of ur button).disabled = true;




我认为您可以禁用客户端代码功能中的按钮,例如以下示例:

Hi,

I think you could disable your button in client code function like example below:


这篇关于如何在执行服务器代码之前禁用ASP.net按钮控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆