michael
michael

Reputation: 15

ASP.net Hidden Field null in Javascript function

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.

Upvotes: 0

Views: 2280

Answers (1)

Win
Win

Reputation: 62260

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" />

Upvotes: 1

Related Questions