1

I use the following Javascript to disable a button after click:

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:

Microsoft JScript runtime error: Unable to set value of the property 'disabled': object is null or undefined

Error Given by script

How can I solve this?

4 Answers 4

1

You can pass the clicked button object in javascript function. Return false if you do not need postback.

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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

and also must return false, or else is doing post back.
Thanks @Aristos i get this error Microsoft JScript runtime error: Unable to set value of the property 'value': object is null or undefined
You need to change document.getElementById(objName).value = msg; to target.value = msg;
@Aristos & Adil yes I disable it for 1 sec. because I mentioned time in my script but after 1 sec I get same error Microsoft JScript runtime error: Unable to set value of the property 'disabled': object is null or undefined
@Ajay When is run the SetTimeOut, Adil here forget to give it an argument again, so its not find it.
|
0

you could do

OnClientClick = "JavaScript: return false; "

also

document.getElementbyid (objectName) 

returns null

are you sure you're passing the correct id of the. button?

Comments

0

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

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

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

Hope this will help you !

Comments

0

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);      
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.