Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 23rd, 2003, 07:25 PM
Registered User
 
Join Date: Aug 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can't Populate Multiple Dropdown Lists with DSO

I am populating dropdown lists via client side scripting using DSO. When I populate one dropdown list, everything is fine. As soon as I try to populate more than one dropdown list with the same database records, I get one populated list and one empty one.

You can test it at http://www.rugbyincanada.com/test/dynamicselect.html

Any suggestions?

Nick

Here's the code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Data Binding Tests</title>
<script language="javascript">
<!--
    function populateLists()
    {
        for(i=1;i<=3;i++)
        {
            populateSelect(eval("document.frmBind.select" + i));
        }
    }

    function populateSelect(objSelect)
    {
        // Since we can't populate a list with databinding, we need to populate it option by option.
        with (objSelect)
        {
            options.length = 0;
            i = 0;
            while (!tdcCustomer.recordset.EOF)
            {
                options[i] = new Option(tdcCustomer.recordset.Fields("ContactName") ,tdcCustomer.recordset.Fields("CustomerID"));
                i++;
                tdcCustomer.recordset.MoveNext();
            }
        }
    }
// -->
</script>
<object classid="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83" ID="tdcCustomer" HEIGHT="0" WIDTH="0" VIEWASTEXT>
  <param name="DataURL" VALUE="customers.csv">
  <param name="UseHeader" VALUE="True">
  <param name="TextQualifier" VALUE="&quot;">
</object>
</head>
<body onload="populateLists();">
<form id="frmBind" name="frmBind">
<table>
<tr>
<td>
<select id="select1" name="select1" datasrc="tdcCustomer" datafld="ContactName" />
</td>
<td>
<select id="select2" name="select2" datasrc="tdcCustomer" datafld="ContactName" />
</td>
<td>
<select id="select3" name="select3" datasrc="tdcCustomer" datafld="ContactName" />
</td>
</tr>
</table>
</form>
</body>
</html>
 
Old September 24th, 2003, 04:06 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

There are two things amiss. Firstly you declare a global variable in populateLists in your loop. You then change this value to more than three in the other function. You need to prefix it with var to make it local to the function:
Code:
for(var i=1;i<=3;i++)
and
Code:
var i = 0;
Secondly you need to move the recordset cursor back to the start each time you use the recordset:
Code:
options.length = 0;
var i = 0;
tdcCustomer.recordset.moveFirst();
while (!tdcCustomer.recordset.EOF)
Another thing is your use of eval. The eval function is costly in terms of time and resources and is almost never needed. Just write:
Code:
populateSelect(document.frmBind[("select" + i)]);
--

Joe
 
Old September 24th, 2003, 07:41 AM
Registered User
 
Join Date: Aug 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Joe,

Thanks for that performance boost. I didn't realize that global vars in javascript were slower than local vars.

I switched the eval as well.

I figured out why only one list was populated. I needed to move to the first record, because after the first populate it statys at EOF.

The one problem I still have and it is very bizarre is the following. If I have more than one select statement in my HTML that has a DSO bound to it, only the first one appears followed by the third and after that it is erratic. However, if they are in a table the seect statements are all rendered on the page. Here is some updated code for you to see what I mean. You can also view the example at http://www.rugbyincanada.com/test/test3.html

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Data Binding Tests</title>
<script language="javascript">
<!--
    function populateLists()
    {
        for(var i=1;i <= (document.forms[0].elements.length + 1);i++)
        //for(var i=1;i <= 4;i++)
        {
            //populateSelect(eval("document.forms[0].select" + i), i);
            populateSelect(document.frmBind["select" + i], i);
        }
    }

    function populateSelect(objSelect, intIndex)
    {
        // Since we can't populate a list with databinding, we need to populate it option by option.
        if (objSelect) {
            with (objSelect)
            {
                options.length = 0;
                var i = 0;
                //Need to start at the first record because after the first lst is populated it stays at the EOF
                tdcCustomer.recordset.MoveFirst();
                while (!tdcCustomer.recordset.EOF)
                {
                    options[i] = new Option(tdcCustomer.recordset.Fields("ContactName") ,tdcCustomer.recordset.Fields("CustomerID"));
                    i++;
                    tdcCustomer.recordset.MoveNext();
                }
                //alert("Finsished populating " + objSelect.name);
            }
        }
        else
        {
            alert("Drop down list " + intIndex + " was not rendered in the page even though it exists in the HTML code. List binding aborted...");
        }
    }
// -->
</script>
</head>
<body onload="populateLists();">
<object classid="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83" ID="tdcCustomer" HEIGHT="0" WIDTH="0" VIEWASTEXT>
  <param name="DataURL" VALUE="customers.csv">
  <param name="UseHeader" VALUE="True">
  <param name="TextQualifier" VALUE="&quot;">
</object>
<form id="frmBind" name="frmBind" onchange="alert(this.name);">
  <p align="center"><strong>There should be 15 Dropdown Lists here (12 in the
    table and 3 before the table), but in reality the 12 in the table appear,
    and only Dropdown lists 1 and 3. I have no idea why 2 does not appear.</strong></p>
  <p align="center">Dropdown List 1<br>
    <select id="select" name="select1" datasrc="tdcCustomer" datafld="ContactName" />
  </p>
  <p align="center">Dropdown List 2 <br>
    <select id="select16" name="select2" datasrc="tdcCustomer" datafld="ContactName" />
  </p>
  <p align="center">Dropdown List 3<br>
    <select id="select17" name="select3" datasrc="tdcCustomer" datafld="ContactName" />
  </p>
  <table width="400" border="1" align="center">
    <tr valign="top">
      <td width="108">Dropdown List 4<br> <select id="select4" name="select4" datasrc="tdcCustomer" datafld="ContactName" />
      </td>
    </tr>
    <tr valign="top">
      <td>Dropdown List 5<br> <select id="select5" name="select5" datasrc="tdcCustomer" datafld="ContactName" />
      </td>
    </tr>
    <tr valign="top">
      <td>Dropdown List 6<br> <select id="select6" name="select6" datasrc="tdcCustomer" datafld="ContactName" />
      </td>
    </tr>
    <tr valign="top">
      <td>Dropdown List 7<br> <select id="select7" name="select7" datasrc="tdcCustomer" datafld="ContactName" />
      </td>
    </tr>
    <tr valign="top">
      <td>Dropdown List 8<br> <select id="select8" name="select8" datasrc="tdcCustomer" datafld="ContactName" />
      </td>
    </tr>
    <tr valign="top">
      <td>Dropdown List 9<br> <select id="select9" name="select9" datasrc="tdcCustomer" datafld="ContactName" />
      </td>
    </tr>
    <tr valign="top">
      <td>Dropdown List 10<br> <select id="select10" name="select10" datasrc="tdcCustomer" datafld="ContactName" />
      </td>
    </tr>
    <tr valign="top">
      <td>Dropdown List 11<br> <select id="select11" name="select11" datasrc="tdcCustomer" datafld="ContactName" />
      </td>
    </tr>
    <tr valign="top">
      <td>Dropdown List 12<br> <select id="select12" name="select12" datasrc="tdcCustomer" datafld="ContactName" />
      </td>
    </tr>
    <tr valign="top">
      <td>Dropdown List 13<br> <select id="select13" name="select13" datasrc="tdcCustomer" datafld="ContactName" />
      </td>
    </tr>
    <tr valign="top">
      <td>Dropdown List 14<br> <select id="select14" name="select14" datasrc="tdcCustomer" datafld="ContactName" />
      </td>
    </tr>
    <tr valign="top">
      <td>Dropdown List 15<br> <select id="select15" name="select15" datasrc="tdcCustomer" datafld="ContactName" />
      </td>
    </tr>
  </table>
  </form>
</body>
</html>


Quote:
quote:Originally posted by joefawcett
 There are two things amiss. Firstly you declare a global variable in populateLists in your loop. You then change this value to more than three in the other function. You need to prefix it with var to make it local to the function:
Code:
for(var i=1;i<=3;i++)
and
Code:
var i = 0;
Secondly you need to move the recordset cursor back to the start each time you use the recordset:
Code:
options.length = 0;
var i = 0;
tdcCustomer.recordset.moveFirst();
while (!tdcCustomer.recordset.EOF)
Another thing is your use of eval. The eval function is costly in terms of time and resources and is almost never needed. Just write:
Code:
populateSelect(document.frmBind[("select" + i)]);
--

Joe
 
Old September 24th, 2003, 08:51 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I've no idea why select2 does not appear, I thought it was because your names and IDs were not matching but any page with two select boxes with one called select2 fails!
As for global and local vars, they're not slower it's just you were changing i in the second function which meant it was never below 3. I'm still checkink the select2 phenomenon.

--

Joe
 
Old September 24th, 2003, 08:55 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Sussed it...
You are using an invalid self closing select tag
Code:
<select/>
IE doesn't understand XHTML even if your dtd declaration is correct. Use the standard syntax.

--

Joe





Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Dropdown lists inside a datagrid gayatricbe ASP.NET 2.0 Basics 3 October 24th, 2007 04:09 PM
Linked Dropdown Lists contagiouss_blue Beginning VB 6 1 June 8th, 2005 10:35 AM
dependent dropdown lists vivshah VS.NET 2002/2003 1 March 29th, 2004 05:25 PM
2 tables and 2 dropdown lists collie Classic ASP Databases 0 October 20th, 2003 07:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.