JavaScript runtime error: Unable to get property 'value' of undefined or null reference

4

I wrote a javascript code to display the next date of a specific calendar

My Function

function verificaDataReferencia(mensagem)
            {   
                if(document.getElementById("txtDataReferencia1Hidden") == null || document.getElementById("txtDataReferencia2Hidden") == null || document.getElementById("ddlDataPub") == null || document.getElementById("txtDataRefInfo") == null)

                var objtxtDataReferencia1Hidden = document.getElementById("txtDataReferencia1Hidden").value;

                var objtxtDataReferencia2Hidden = document.getElementById("txtDataReferencia2Hidden").value;

                > Breakpoint var objtxtDataArquivo = document.getElementById("ddlDataPub").value;

                var mensagem = document.getElementById("txtDataRefInfo").value;
                if((objtxtDataReferencia1Hidden == objtxtDataArquivo) || (objtxtDataReferencia2Hidden == objtxtDataArquivo))
                {
                    var x = alert(mensagem);
                    return x;
                }
            }

Html

<asp:TextBox style="Z-INDEX: 112; POSITION: absolute; TOP: 9px; LEFT: 572px" id="txtDataReferencia1Hidden"
                                            runat="server" CssClass="inputLabel" Width="15" Height="15px"></asp:TextBox>
                                        <asp:TextBox style="Z-INDEX: 113; POSITION: absolute; TOP: 9px; LEFT: 606px" id="txtDataReferencia2Hidden"
                                            runat="server" CssClass="inputLabel" Width="14" Height="14px"></asp:TextBox>

the problem is that the page is running well in chrome but when i run my application in IE it throw an error

0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference

please help me to overcome from this problem.

private void Page_Load(object sender, System.EventArgs e)
        {

            if(! IsPostBack)
            {   
                txtNomePortugues.SetFocus();
                BO.Pasta pasta = new BO.Pasta(CodPasta);
                if (CodigoArquivo == 0 && !IsJustificativa && pasta.EnviaEmail)
                    btnGravar.Attributes.Add("onclick", "verificaDataReferencia(); confirmaEnvioEmail('" + GetString("Mensagem_Confirmacao_Alerta_Arquivos") + "');");
                else
                    btnGravar.Attributes.Add("onclick", "verificaDataReferencia();");
javascript
html
asp.net
asked on Stack Overflow Apr 30, 2015 by tpires01 • edited Apr 30, 2015 by tpires01

6 Answers

2

The issue is here:

var mensagem = document.getElementById("txtDataRefInfo").value;
var objtxtDataReferencia1Hidden = document.getElementById("txtDataReferencia1Hidden").value;
var objtxtDataReferencia2Hidden = document.getElementById("txtDataReferencia2Hidden").value;

Reason being txtDataRefInfo, txtDataReferencia1Hidden, txtDataReferencia2Hidden is a server side control and not the client side. Try this:

var objtxtDataReferencia1Hidden = document.getElementById('<%=txtDataReferencia1Hidden.ClientID%>');
var objtxtDataReferencia2Hidden = document.getElementById('<%=txtDataReferencia2Hidden.ClientID%>');
var mensagem = document.getElementById('<%=txtDataRefInfo.ClientID%>');
answered on Stack Overflow Apr 30, 2015 by Rahul Tripathi • edited Apr 30, 2015 by Rahul Tripathi
2

You need to set the ClientIDMode attribute on the control to:

ClientIDMode="Static"

This works with .Net 4.0 and upwards

answered on Stack Overflow Jul 26, 2016 by Jerome • edited Jul 26, 2016 by Pureferret
0

comment this line for IE it may be ok :

// > Breakpoint var objtxtDataArquivo = document.getElementById("ddlDataPub").value;
answered on Stack Overflow Apr 30, 2015 by MacKentoch
0

The problem is that txtDataReferencia1Hidden and txtDataReferencia2Hidden are the server name of controls, not the client name (look the source of page from the browser).

Use this:


 var objtxtDataReferencia1Hidden = document.getElementById('<%=txtDataReferencia1Hidden.ClientID%>');


 var objtxtDataReferencia2Hidden = document.getElementById('<%=txtDataReferencia2Hidden.ClientID%>');
answered on Stack Overflow Apr 30, 2015 by Vizllx
0

there is missing an action behind this if(document.getElementById("txtDataReferencia1Hidden") == null || document.getElementById("txtDataReferencia2Hidden") == null || document.getElementById("ddlDataPub") == null || document.getElementById("txtDataRefInfo") == null)

I would expect you write the line in this way (do not forget the negation using !):

if(!document.getElementById("txtDataReferencia1Hidden") || !document.getElementById("txtDataReferencia2Hidden") || !document.getElementById("ddlDataPub") || !document.getElementById("txtDataRefInfo")) return;

answered on Stack Overflow Apr 30, 2015 by Pavel Gatnar
0

You are checking to see if the elements are null in your if statement -

if(document.getElementById("txtDataReferencia1Hidden") == null || document.getElementById("txtDataReferencia2Hidden") == null || document.getElementById("ddlDataPub") == null || document.getElementById("txtDataRefInfo") == null)

But even if they are null you are trying to get the value. Ex:

var mensagem = document.getElementById("txtDataRefInfo").value;

So if "txtDataRefInfo" is null the statement will return true and try to get the value anyways. Hence the undefined or null reference.

answered on Stack Overflow Apr 30, 2015 by mac

User contributions licensed under CC BY-SA 3.0