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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign Values to <SELECT> CONTROL

Status
Not open for further replies.

sparkyrocks

Programmer
Aug 2, 2002
2
US
I am retrieving data from a database and creating an
XML file which is valid and well formed. When I run my
.asp page I am attempting to fill a select box with
the data contained in the xml file as follows

1. A client side VBScript sub routine is fired when
the <BODY ONLOAD = &quot;FillCombo&quot;>

2. I create the object and load the xml file as
follows

Set doc = CreateObject(&quot;Microsoft.XMLDOM&quot;)
doc.async = False
doc.load &quot;XML/coopdata.xml&quot;

3. The xml file contains these nodes which I want to
use to populate a select box:
<code>CLS</code>
<code>YP</code>
<codePRT</code>
<code>OTH</code>
So I get these values by
Set codes = doc.getElementsByTagName(&quot;code&quot;)
To determine if I am retrieving the data I perform
this test in my script by looping thru the collection
For Each code in codes
str = str & code.firstChild.nodeValue & vbCrlf
Next
MsgBox str

The message box contains all the values so at this
point everything is hunky dory. The problem is when I
try to dynamically attempt to assign these values to
an empty select box with the following

i=0
For Each code in codes
document.frmAddClaim.cboMediaType.options(i).value =
code.firstChild.nodeValue
i = i+1
Next

I get this error
Error:Object required
document.frmAddClaim.cboMediaType.options(...)

I believe this error message is telling me that

select.option(0) does not exist. So in my next test I
hard coded one value in the select box

<option value = &quot;Test&quot;>
Test
</option>

Then I adjusted my loop to dynamically assign the
value from the collection to the 1 and only <option>
in the select box

For Each code in codes
document.frmAddClaim.cboMediaType.options (0).value =
code.firstChild.nodeValue
document.frmAddClaim.cboMediaType.options(0).text =
code.firstChild.nodeValue
Next

And it worked. So what this is telling me is that
client side script can only assign values to <option>
tags that already exist. I do not know how to create
client side script that creates the <option> tags
dynamically for the select box so I can populate them
with values from the codes collection.

So then I decided to attempt server side script (which
is my background). Typically I would grab data from a
recordset and loop thru it to create all the <option>
tags for the select box. This is the snippet of server
side code

Set doc = Server.CreateObject(&quot;Microsoft.XMLDOM&quot;)
doc.async = False
doc.load &quot;XML/coopdata.xml&quot;
Set codes = doc.getElementsByTagName(&quot;code&quot;)
For Each code in codes
str = str & code.firstChild.nodeValue & vbcrlf
Next
Response.Write str

As you can see I am attempting to pull data from my
codes collection. For some reason the variable str is
an empty string when I attempt this server side. No
.asp errors are generated. So then I changed the code
to

doc.load &quot;XML/coopda.xml&quot;

I figured this would generate an error since no such
file exists. No error was returned. So then I changed
the code so the load method would accept a full path

doc.load &quot;C:\Bruce\Coop Advertising\Web
Application\XML\coopdata.xml&quot;

Still no data from the collection.

To summarize my dilemna is the following

1. When I run client side script I can use DOM to
retrieve the data but I have no idea how to get the
data into a select box.

2. When I run server side script I cannot retrieve the
data but I know how to get data into a select box.


Can someone help? Any assistance would
be greatly appreciated.

 
to create a new option (as far as i remember from DOM), use the add() method from the options collection object to add it. eg

Set Element=document.createElement(&quot;OPTION&quot;)

Element.text=code.firstChild.nodeValue
Element.value=Element.text

document.frmAddClaim.cboMediaType.options.add Element


Hope that helps.

matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top