chewie
chewie

Reputation: 333

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

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();");

Upvotes: 4

Views: 31231

Answers (6)

Jerome
Jerome

Reputation: 21

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

ClientIDMode="Static"

This works with .Net 4.0 and upwards

Upvotes: 2

mac
mac

Reputation: 504

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.

Upvotes: 0

Pavel Gatnar
Pavel Gatnar

Reputation: 4053

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;

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172448

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%>');

Upvotes: 2

Vizllx
Vizllx

Reputation: 9246

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%>');

Upvotes: 0

MacKentoch
MacKentoch

Reputation: 2446

comment this line for IE it may be ok :

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

Upvotes: 0

Related Questions