开发者

Why am I getting this "null or not an object" error in IE?

开发者 https://www.devze.com 2023-01-31 04:13 出处:网络
I have a button (<asp:button id=\"MyButton\" runat=\"server\" />). I want this button to be automatically开发者_开发百科 clicked onload. So I added JavaScript for that to happen:

I have a button (<asp:button id="MyButton" runat="server" />). I want this button to be automatically开发者_开发百科 clicked onload. So I added JavaScript for that to happen:

<script language="javascript" type="text/javascript">
      var myButton = document.getElementById('myButton');
      myButton.click();
</script> 

The error I'm getting is "Microsoft JScript runtime error: 'myButton' is null or not an object"

The page this button is on has a MasterPage in the back. Would that be the reason this is happenening?


document.getElementById() is case sensitive. Try document.getElemenById('MyButton').


You want to write the client ID rather than the server ID.

<script language="javascript" type="text/javascript">
      var myButton = document.getElementById('<%=MyButton.ClientID%>');
      myButton.click();
</script> 

I see, this code will run before the button is loaded on the client so you will need to place this script block after the button or if you are using jQuery or something similar they will offer document ready functionality. The following code will be run once the document has loaded completely.

$(document).ready(function () {
    var myButton = $('#<%=MyButton.ClientID%>');
    myButton.click();
});

You'll want to set a few properties to implement your requirements.

<asp:button 
    id="MyButton" 
    runat="server" 
    UseSubmitBehavior="false" 
    OnClientClick="return changeBackgroundColor();" />

Setting the UseSubmitBehavior to false means that the button will not post back to the server. The OnClientClick property is as literal as it sounds. When clicked the client will execute the JavaScript code specified.


What is the Id of the button in the rendered HTML on the client side? ASP .NET has a tendency to have longer auto-generated client-side IDs for its own use. One thing you could do is set the name attribute of the button and reference that in the JavaScript.

Also of potential use to you is a method of finding a .NET client ID using jQuery.


The button probably has another ID when it's rendered to the client. Try to set ClientIDMode="Static" for the button. Then the id will be then same as on the server side.


Change the id="MyButton" to id="myButton" case matters in javascript/DOM


The page may not have loaded fully so when this code is executed 'myButton' may not be available yet. You can either make a function that calls this on body load:

<body onload="functionForButton()">

or put your script at the end of the body tag:

<script language="javascript" type="text/javascript">
      var myButton = document.getElementById('myButton');
      myButton.click();
</script> 
</body>

Also make sure the case is the same. MyButton != myButton


Change your script to :

<script language="javascript" type="text/javascript">
    var myButton = document.getElementById('<%= myButton.ClientID %>');
    myButton.click();
</script>

And put this script at the end of page just before </body>.

0

精彩评论

暂无评论...
验证码 换一张
取 消