0

have a question about referencing an

 <asp:HiddenField ID="editcheck" runat="server"/>

from a JS function. The function is being hit as a null reference error is thrown by the

var e = document.getElementById('<%=editcheck.ClientID%>');

line in the function.

Any Ideas?

Thanks

ps: Here is the actual line throwing the exception.

 if(e.value == "true")
     return confirm("yadayad");

The error states value can not be checked on a null object, or something close. So that is why I'm asking about the JS function seeing the element.

5
  • That particular line cannot throw null reference in JavaScript land unless document is null, which is unlikely. Is this the real code? What variable exactly is null? Commented May 9, 2016 at 14:39
  • Actually, you are correct. the exception is on the following line where I check for value. but when I hover over the element variable 'e' the debugger also shows null. Here is the actual line throwing the exception. if(e.value == "true") return confirm("yadayad"); It error states value can not be checked on a null object, or something close. So that is why I'm asking about the JS function seeing the element. Thanks. Commented May 9, 2016 at 14:47
  • the line did not make it into the comment. anyway, please edit it into the question, since it is an important part of it. Commented May 9, 2016 at 14:49
  • When and how is your JavaScript code that throws the exception called? Commented May 9, 2016 at 15:25
  • var e = document.getElementById('<%#editcheck.ClientID%>'); Commented May 9, 2016 at 15:34

1 Answer 1

1

I'm guessing that you call the script before HiddenField is rendered to the browser.

Could you ensure that script is called after the HiddenField?

<asp:HiddenField ID="editcheck" runat="server"/>

document.getElementById('<%=editcheck.ClientID%>');

OR you can use jQuery which is a lot easier if you have to manipulate with DOM. The following script doesn't matter where you place the HiddenField in the page.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>    
<script type="text/javascript">
    $(function () {
        if ($("#<%= editcheck.ClientID %>").val() === "true") {
            return confirm("yadayad");
        }
    });
</script>        
<asp:HiddenField ID="editcheck" runat="server" />
Sign up to request clarification or add additional context in comments.

6 Comments

Hmm, you may be on to something. I am registering the function from the master page. though I call it from an onClientClick of a button on the actual page., Maybe I should try re-registering it on page_Load event of the page? I'll get back with you. Thanks!
Nope re-registered it in page_load. no difference. I could try putting the script in the page but. I have not had to do that with other scripts. but for grins and giggles I'll move it.
I added the jQuery script, and see it help.
Thanks for sticking with me. I've moved my script to the page itself. I get script undefined errors. Then I pasted your script in place of mine. Still getting script undefined.
Ok. You might want to debug step-by-step. First of all, you want to create a standalone aspx page without master page, and paste jQuery script including jquery.min.js.
|

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.