添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I use JavaScript and this error appears for me during execution:

Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object

this my code:

<asp:Content ID="content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script language="javascript" type="text/javascript">
    function ConfirmTransfere() {
        if (confirm("Syatem not allow negative inventory, would you like to continue ?") == true) {
            document.getElementById("btnAlelrt").click();
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="uxContainer" runat="server">
    <ContentTemplate>
 <table> 
        <asp:Button ID="uxTransfer" runat="server" Text="Transfer" OnClick="uxTransfer_Click" /> 
        <asp:Button ID="btnAlelrt" runat="server" Text="GetDetails" OnClick="btnAlelrt_Click" />       
</table>
    </ContentTemplate>
<Triggers>
    <asp:PostBackTrigger ControlID="uxTransfer" />    
    </Triggers>
    </asp:UpdatePanel>
 </asp:Content>
function ConfirmTransfere() {
        if (confirm("Syatem not allow negative inventory, would you like to continue ?") == true) {
            document.getElementById("btnAlelrt").click();

needs to be this:

function ConfirmTransfere() {
        if (confirm("Syatem not allow negative inventory, would you like to continue ?") == true) {
            document.getElementById('<%=btnAlert.ClientID%>').click();

The problem is that your button is an ASP.Net control and will generate it's own client id, which will be different from the ID you specified. putting in the code <%=btnAlert.ClientID%> Will post the generated one out to the browser.

Starting from ASP.NET 4.0 you can use ClientIDMode property for you element. And if you set it into Static then the ClientID value will be set to the value of the ID property:

<asp:Label ID="Label1" runat="server" ClientIDMode="Static" />

will be rendered as something like this:

<span id="Label1" name="ctl00$MasterPageBody$ctl00$Label1" />
                Thank you, the ClientIDMode="Static" save my problem because I was not able to get the div in javascript.
– davidlebr1
                Oct 1, 2014 at 20:29

The ID you give the asp:Button is the ID that is used on the server by ASP.Net. It is not the ID that is used by the client script.

You should use:

document.getElementById("<%= btnAlelrt.ClientID %>").click();

If you view source on your generated page you will likely find that the button no longer has the ID "btnAlelrt". It might have something like "ctl001_btnAlert", or some other generated uniqueness identifier.

This is why your javascript isn't finding it.

You'll either need to search by tag, and check the IDs to see if they end in btnAlelrt, or you'll have to place a DIV or SPAN around it with an ID that won't be changed (i.e. doesn't have runat="server"). You can then find that element by its ID, and then get the button inside of it.

If you can, you can use the <%=btnAlelrt.ClientID%> syntax to insert the proper ID in your javascript.

(SIDENOTE:was this supposed to be btnAlert?)

I can't find a code sample to post for you at this moment, but I can explain the problem your having.

ASP.NET doesn't use the ID you give an object /element / button when it writes it to the page, instead it give the object a client ID.

If you do Msgbox(btnAlelrt.clientID) in your code then you will see the object name your javascript should use.

It is better to have the .NET code write out these values, in javascript, to the page, as they are not a constant.

Hope this helps.

James

try using ClientIDMode="Static". this will dont change button id at runtime.

  <asp:Button ID="SendMessageButton" ClientIDMode="Static" runat="server" Text="Send Message" CssClass="buttonPositive"
            CausesValidation="True" OnClientClick="validate();" OnClick="SendMessageButton_Click" />

Then you can use below jquery to access your button. I hope this helps.

 var element = document.getElementById('SendMessageButton');
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.