简体   繁体   English

Javascript禁用asp.net按钮

[英]Javascript to disable asp.net button

I use the following Javascript to disable a button after click: 单击后,我使用以下Javascript禁用按钮:

var c = 0;
function DisableClick(target) {
    var objName = target;
    document.getElementById(objName).disabled = true;
    c = c + 1;
    msg = 'Please Wait...(' + c + ')!';
    document.getElementById(objName).value = msg;
    var t = setTimeout('DisableClick()', 1000);
}

<asp:Button ID="btnLogin" runat="server" CssClass="cssLoginButton blue"  Text="Log in" ToolTip="Log in" ValidationGroup="UserLogin" onclick="btnLogin_Click" OnClientClick="DisableClick('btnLogin')" />

My Javascript gives this error: 我的Javascript出现此错误:

Microsoft JScript runtime error: Unable to set value of the property 'disabled': object is null or undefined Microsoft JScript运行时错误:无法设置属性“ disabled”的值:对象为null或未定义

脚本给出的错误

How can I solve this? 我该如何解决?

You can pass the clicked button object in javascript function. 您可以在javascript函数中传递被单击的按钮对象。 Return false if you do not need postback. 如果不需要回发,则返回false。

Change 更改

OnClientClick="DisableClick('btnLogin')" 

To

// the DisableClick make a loop, so make the return here.
OnClientClick="DisableClick(this);return false;" 


function DisableClick(target) {   
    target.disabled = true;
    c = c + 1;
    msg = 'Please Wait...(' + c + ')!';
    target.value = msg;

    // The DisableClick needs the target parametre, so send it again
    //  but need to keep it here for work.
    var me = target;
    var t = setTimeout(function(){DisableClick(me);}, 1000);
}

you could do 你可以做

OnClientClick = "JavaScript: return false; "

also

document.getElementbyid (objectName) 

returns null 返回null

are you sure you're passing the correct id of the. 您确定要传递正确的ID。 button? 按钮?

What you can do is call the function on load of the body and it should work fine. 您可以做的是在身体负荷时调用该函数,它应该可以正常工作。 So the code will be 所以代码将是

<script type="text/javascript">
function disableBtn()
{
var btn= document.getElementById("<%=Button1.ClientID%>");
btn.disabled=true;
}
</script>

Html HTML

<body onload="javascript:disableBtn();">

 <asp:Button ID="Button1" runat="server" Text="Button" />
 ...
 </body>

Hope this will help you ! 希望这个能对您有所帮助 !

Details: This should solve your solution of Disabling the button as well as displaying a message until the process has finished 详细信息:这应该可以解决您的禁用按钮以及显示消息直到解决方案完成的解决方案

Markup 标记

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
     <ContentTemplate>

      .... Your other code

        <asp:Button ID="btnLogin" runat="server" CssClass="cssLoginButton blue"  Text="Log in" ToolTip="Log in"
           OnClientClick="this.disabled=true;" ValidationGroup="UserLogin" onclick="btnLogin_Click"  />

     </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdateProgress ID="UpdateProgress1" runat="server">
        <ProgressTemplate>
            Please Wait...
        </ProgressTemplate>
    </asp:UpdateProgress>

To test it 测试一下

Code behind 后面的代码

protected void btnLogin_Click(object sender, EventArgs e)
{
   // Introducing delay for demonstration.
   System.Threading.Thread.Sleep(3000);      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM