In an ASP.NET page, if you get an error similar to "BasicDatepicker is undefined" or "BDPLite is undefined", it may be because you have code that is attempting to set focus to the BasicDatePicker control.
If you have JavaScript code that performs a ".focus()" on an instance of the BasicDatePicker control, this code is likely to be called before the BasicDatePicker control has been created on the page. This can happen if you call ".focus()" in inline JavaScript or in an ASP.NET "RegisterClientScriptBlock" method (which creates the JavaScript block immediately after the first <form> tag).
The "undefined" error message may happen even if you add a time delay (using the setTimeout JavaScript method) before calling the ".focus()" method, depending on the speed of the client machine and the web server and the complexity of the page.
The solution to this issue/error is to ensure that the page is fully loaded before calling the ".focus()" method. The simplest approach is to call the ".focus()" method for the BasicDatePicker control in the body onload event.
However, you may already have code that is called in the body onload event, and calling ".focus()" from the body onload tag would break your existing code. If you want a more general method to add a body onload call when there may already be an onload call, you can use the following approach (I found this idea somewhere on the web, but I don't remember where):
(1) Include this JavaScript code in a common file (this either creates an onload function or appends the new onload function to any existing calls).
function addWindowLoadEvent(func)
{
var oldonload = window.onload;
if (typeof window.onload != 'function')
{
window.onload = func;
}
else
{
window.onload = function()
{
oldonload();
func();
}
}
}
(2) In the ASP.NET page or event where you want to set focus to the BasicDatePicker control, add the following ASP.NET code (shown here in C#).
System.Text.StringBuilder jScript = new System.Text.StringBuilder();
jScript.Append("<script language=\"JavaScript\">\n");
jScript.Append("addWindowLoadEvent(function(){ setFocusToDesiredElement();} );");
jScript.Append("</script>");
this.RegisterClientScriptBlock("OnloadSetFocus",jScript.ToString());
Also, write the relevant JavaScript code for "setFocusToDesiredElement" (to find the appropriate BasicDatePicker element and set focus to it) - this may be as simple as "document.getElementById("MyBDPcontrol").focus()".