Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Typing value for Dropdownlist value only uses first value 1

Status
Not open for further replies.

lanm

Programmer
Joined
Jul 7, 2005
Messages
244
Location
US
If I have dropdownlist values, and start type something like: "Ascott", I hit the "A" values on the first letter, but then start way down at the "S" values on the second letter entry of "s".

So, when I hit "a", I go to "Abraham" in the list. Now, if I hit "s", I'd like to be taken to "Ascott", but I'm taken all the way down to "Samuel" in the dropdownlist.

How can I fix this?

Thanks!
 
i use this javascript and it seems to work pretty well..

Code:
var keys;
var timeStamp;
timeStamp = new Date();

function dd_onkeypress(ddListName) {
	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
		if (diff > 1000) {
			keys = key;	
		} else {
			keys = keys + key;
		}
		var cnt;
		for (cnt=0;cnt<document.all(ddListName).children.length;cnt++) {
			var itm = document.all(ddListName).children[cnt].text;
			if (itm.substring(0,keys.length).toLowerCase()==keys.toLowerCase()) {
				document.all(ddListName).selectedIndex = cnt;
				break;
			}
		}
	}
	document.all(ddListName).onchange();
}


ddTEAMPLATE.Attributes.Add("onkeypress", "javascript:return dd_onkeypress('ddTEAMPLATE');")

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Thanks checkai!

I'll try this and let you know!
 
checkai,

Does this go in a class file/component, or in the HTML page?

Thanks!
 
it's in a javascript file that I reference in my .aspx page...then within the code-behind I link the drop down lists onkeypress event to it...

Code:
dropdown.Attributes.Add("onkeypress", "javascript:return dd_onkeypress('dropdown');")

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Okay, so you named the javascript file as ddtemplate?
 
checkai!

Sorry to be asking so many questions.
I named my file just JScrip1.js, and my dropdownlist is simply dropdownlist1.

Now, do you have to call something in Page_Load, and also call something in the dropdownlist_Load handler?
 
within your page load you can then put this code...

Code:
dropdownlist1.Attributes.Add("onkeypress", "javascript:return dd_onkeypress('dropdownlist1');")

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
checkai,

I've added the following in Page_Load, and keep getting a script error:

DropDownList1.Attributes.Add("onkeypress", "javascript:return JScript1('DropDownList1');")
 
Okay, messed up on the code. Should be:

DropDownList1.Attributes.Add("onkeypress", "javascript:return dd_onkeypress('DropDownList1');")

In the .js file, I've got:
function dd_onkeypress(DropDownList1) {
 
...but, still get snytax error.
 
checkai,

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

Thanks for the help!
 
try changing this...

Code:
DropDownList1.Attributes.Add("onkeyup", "javascript:return dd_onkeypress(this);")

to this...
Code:
DropDownList1.Attributes.Add("onkeyup", "javascript:return dd_onkeypress('DropDownList1');")


"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
checkai!

Tried that too.

The line 15 error is in the HTML side of the .aspx page, which points to this asp:dropdownlist

Any other suggestions?

Here's what I've got in the .js file so far:

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++)
                       for (cnt=0;cnt<DropDownList1.children.length;cnt++)
                       {
                       //var itm = document.all(DropDownList1).children[cnt].text;
                       var itm = DropDownList1.children[cnt].text;
                       if (itm.substring(0,keys.length).toLowerCase()==keys.toLowerCase())
{
//document.getElementById(DropDownList1).selectedIndex = cnt;
DropDownList1.selectedIndex = cnt;
break;
}
}
}
//document.all(DropDownList1).onchange();
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top