akvickyit7 akvickyit7 - 4 months ago 22
ASP.NET (C#) Question

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

i have written a javascript code to compare 2dates from 2 textboxes

function CompareDates() {
var fdate = document.getElementById('txtFromDate');
var edate = document.getElementById('txtToDate');
var FromDate = fdate.value.split('/');
var EndDate = edate.value.split('/');
var val = 'false';

if (parseInt(FromDate[2]) < parseInt(EndDate[2])) {
val = 'true';
return true;
}
else if (parseInt(FromDate[2]) == parseInt(EndDate[2])) {
if (parseInt(FromDate[0]) < parseInt(EndDate[0])) {
val = 'true';
return true;
}
else if (parseInt(FromDate[0]) == parseInt(EndDate[0])) {
if (parseInt(FromDate[1]) <= parseInt(EndDate[1])) {
val = 'true';
return true;
}
}
}
if (val == "false") {
alert("FromDate Always Less Than ToDate");
return false;
}
}


and html code is

<asp:TextBox ID="txtFromDate" runat="server" Width="150px" />
<ajaxToolkit:CalendarExtender CssClass="MyCalendar" runat="server"
ID="ceFromDate"
TargetControlID="txtFromDate"
Format="dd/MM/yyyy" />
<asp:TextBox ID="txtToDate" runat="server" Width="150px" />
<ajaxToolkit:CalendarExtender CssClass="MyCalendar" runat="server"
ID="ceToDAte"
TargetControlID="txtToDate"
Format="dd/MM/yyyy" />

<asp:Button ID="btnGenerate" runat="server" CssClass="button"
Text="Generate" OnClientClick="if(!CompareDates()) return false;"
OnClick="btnGenerate_Click" />


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.

Answer

The error is here:

    var fdate = document.getElementById('txtFromDate');
    var edate = document.getElementById('txtToDate');

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

Try this:

    var fdate = document.getElementById('<%=txtFromDate.ClientID%>');
    var edate = document.getElementById('<%=txtToDate.ClientID%>');