Click here to Skip to main content
15,867,985 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I need to disable an ASP.net button using javascript on pageload
my code as follows
//====================================================
XML
<script type="text/javascript">
    function disableBtn()
    {
    var btn= document.getElementById("<%=Button1.ClientID%>");
    btn.disabled=true;
    }
    window.onload=disableBtn();
    </script>

//==================================================
<asp:Button ID="Button1" runat="server" Text="Button" />
//=======================================================
Error: Microsoft JScript runtime error: 'null' is null or not an object
Posted
Updated 18-Feb-23 17:12pm
Comments
Steven.Pinto2000 10-May-11 8:32am    
just debug java script and see to see how to debug java script check this link
http://www.codeproject.com/Tips/190937/How-to-fix-JavaScript-Errors.aspx

That's because you have called the function on load of the window and the Button element is not available at that time (and thus 'null').

What you can do is call the function on load of the body and it should work fine. So the code will be
XML
<script type="text/javascript">
    function disableBtn()
    {
    var btn= document.getElementById("<%=Button1.ClientID%>");
    btn.disabled=true;
    }
    //window.onload=disableBtn(); //NOTE: I have commented this line
</script>

XML
<body onload="javascript:disableBtn();">
...
...
<asp:Button ID="Button1" runat="server" Text="Button" />
...
</body>


Hope this helps!
 
Share this answer
 
Comments
SHAJANCHERIAN 10-May-11 13:17pm    
Thank You Ankur.
Ankur\m/ 11-May-11 0:15am    
You are welcome!
thatraja 10-May-11 14:04pm    
Spot on, 5
Ankur\m/ 11-May-11 0:15am    
Thanks, Raja!
how to do it from code behhind?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900