Trying to use .js file for dropdownlist autocomplete


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Trying to use .js file for dropdownlist autocomplete

  1. #1
    Join Date
    Aug 2005
    Posts
    79

    Trying to use .js file for dropdownlist autocomplete

    I'm trying to use a .js file to get autocomplete on a dropdownlist control in my ASP.NET application, but keep getting a syntax error.

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

  2. #2
    Join Date
    Aug 2005
    Posts
    23
    keys is never defined as an object type

    so if it gets to this line
    Code:
     keys = keys + key;
    it has no value to + key upon.

    perhaps just doing:
    Code:
    var keys = '';
    could help your code.

  3. #3
    Join Date
    Aug 2005
    Posts
    79
    KC-Luck,

    We found that the line it's pointing to is actually the line in the HTML code for the .aspx page. If we move the asp:dropdownlist tag down a line, then the error line goes to line 16.

  4. #4
    Join Date
    Aug 2005
    Posts
    23
    right on, plus on second look it appears keys would always become defined, unless the user clicked upon before the first 1 second
    any link to the rendered page?
    Last edited by KC-Luck; 08-25-2005 at 06:05 PM.

  5. #5
    Join Date
    Aug 2005
    Posts
    79
    Nah, it's pretty secure. I wish since this is killin' me! I still get an "Object Expected" on the line that points to the dropdownlist in the HTML.

    In the Page_Load:
    DropDownList1.Attributes.Add("onkeypress", "javascript:dd_onkeypress(this);return false;")

    Then .js file is:
    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++)
    * *********************for (cnt=0;cnt&lt;DropDownList1.options.length;cnt++) {
    ********************** var itm = DropDownList1.options[cnt].text;
    ********************** if
    (itm.substring(0,keys.length).toLowerCase()==keys.toLowerCase())
    * *********************{
    * *********************//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();
    }

  6. #6
    Join Date
    Aug 2005
    Posts
    23
    will it run without the javascript: put in before the dd_...

  7. #7
    Join Date
    Aug 2005
    Posts
    79
    If I comment out the Page_Lode code which calls the .js file, yeah, it just acts as normal picking up the first letter of the entry. Typing with the intention of "Abraham" takes me to "Aalcot" then "Babylon" or something along those lines. Not to Abraham.

  8. #8
    Join Date
    Aug 2005
    Posts
    23
    sorry not following that your Page_Load executes that .js file above?
    you shouldn't need the javascript: beginning of Attributes.Add tried it here successfully.

    onload for DropDownList1
    DropDownList1.Attributes.Add("onkeypress","alert(event.keyCode)");

    and the lines with document.all(DropDownList1).... don't need the document.all()..

    furthermore, i would replace the argument name with just list or something,
    and then list would already be the valid reference to your DropDownList1,
    sometimes IE doesn't like you to have/create variable of same element ID.
    Last edited by KC-Luck; 08-25-2005 at 06:35 PM.

  9. #9
    Join Date
    Aug 2005
    Posts
    23
    Code:
    var keys = '', timeStamp = new Date();
    
    function dd_onkeypress(list) {
      var i = 0, key = window.event.keyCode;
      window.event.returnValue = false;
      if((key>=97 && key<=122)||(key>=65 && key<=90)||(key>=48 && key<=57)){
        key = String.fromCharCode(key);
        var diff = (new Date).getTime()-timeStamp.getTime();
        keys = keys + (diff > 1000 ? '' : key);
        timeStamp = (new Date);
      }
      var c = list.childNodes;
      for(i = 0; i < c.length; i++) {
        var s = (c[i].text||c[i].innerText);
        if(s.substring(0,keys.length).toLowerCase()==keys.toLowerCase()){
          list.selectedIndex = i;
          break;
        }
      }
    }

Similar Threads

  1. wav file fade in out help
    By jase_dukerider in forum C++
    Replies: 2
    Last Post: 04-14-2005, 08:48 PM
  2. How long before the next version??
    By _CAG in forum .NET
    Replies: 146
    Last Post: 08-12-2002, 11:40 PM
  3. File uploading / objFile.Write problem
    By Tomer Cagan in forum ASP.NET
    Replies: 1
    Last Post: 07-24-2001, 10:01 AM
  4. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 06:34 PM
  5. Replies: 0
    Last Post: 08-22-2000, 11:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center