Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Trying to use .js file for dropdownlist autocomplt

Author  Topic 

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2005-08-23 : 16:38:02
Here's the .js file I'm trying to use on DropDownList1 to get it to autocomplete for easier searching (when I run the page I get a syntax error on line 15, which is keys = key;

var keys;
var timeStamp;
timeStamp = new Date();
function dd_onkeypress(DropDownList1) {
var key = event.keyCode;
event.returnValue=false;
//a-z, A-Z, 0-9
if ((key>=97 && key<=122) || (key>=65 && key<=90) || (key>=48 && key<=57)) {
key = String.fromCharCode(key);
var now = new Date();
var diff = (now.getTime() - timeStamp.getTime());
timeStamp = new Date();
//1 seconds = 1000 milliseconds<BR>
if (diff > 1000) {
keys = key;
} else {
keys = keys + key;
}
var cnt;
for (cnt=0;cnt<document.all
(DropDownList1).children.length;cnt++) {
var itm = document.all(DropDownList1).children[cnt].text;
if (itm.substring(0,keys.length).toLowerCase()==keys.toLowerCase())
{
document.all(DropDownList1).selectedIndex = cnt;
break;
}
}
}
document.all(DropDownList1).onchange();
}

On the Page_Load handler, I've got:
DropDownList1.Attributes.Add("onkeypress", "javascript:return dd_onkeypress('DropDownList1');")

When I run the page, and start typing on the dropdownlist, I get:
An error has occured in the script on this page. Object Expected

MichaelP
Jedi Yak

2489 Posts

Posted - 2005-08-23 : 17:19:36
Proably because there's no object called DropDownList1.
In .net, it's called that, but when it's written to the browser, it gets a new name. There's a way to get the name that it;s going to have, but I can't remember off the top of my head. Look at the properties of that DropDownList. You should be able to find it.

Michael

<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>
Go to Top of Page

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2005-08-24 : 11:53:59
MichaelP,

I'm trying the following, but the error on the .js file is:

Line 15, Char 1, Object Expected:

var keys;
var timeStamp;
timeStamp = new Date();
function dd_onkeypress(DropDownList1) {
var key = event.keyCode;
event.returnValue=false;
//a-z, A-Z, 0-9
if ((key>=97 && key<=122) || (key>=65 && key<=90) || (key>=48 && key<=57)) {
key = String.fromCharCode(key);
var now = new Date();
var diff = (now.getTime() - timeStamp.getTime());
timeStamp = new Date();
//1 seconds = 1000 milliseconds<BR>
if (diff > 1000) {
keys = key;
} else {
keys = keys + key;
}
var cnt;
for (cnt=0;cnt<document.all
(DropDownList1).children.length;cnt++) {
var itm = document.all(DropDownList1).children[cnt].text;
if (itm.substring(0,keys.length).toLowerCase()==keys.toLowerCase())
{
document.getElementById(DropDownList1).selectedIndex = cnt;
break;
}
}
}
//document.all(DropDownList1).onchange();
}

In my Page_Load .aspx page, I've got:
DropDownList1.Attributes.Add("onkeyup", "javascript:return dd_onkeypress(this);")
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2005-08-24 : 16:39:56
I'm not sure what line 15 is there, but i'm pretty sure this will not work

document.getElementById(DropDownList1).selectedIndex = cnt;


change that to
DropDownList1.selectedIndex = cnt;

Since you are passing an object reference using "this" then you don't need to get element by id, becuase you HAVE the element.

Michael

<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>
Go to Top of Page

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2005-08-25 : 17:20:11
The line where the "Object Expected" is actually the HTML of the .aspx page where the asp:dropdownlist is located.

Not sure if this helps!

Thanks!
Go to Top of Page
   

- Advertisement -