473,426 Members | 4,299 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,426 software developers and data experts.

Populate a combo with an array

Hi,

I have an array - ListOfFiles - that i want use to populate an combo.

I've attempted to do it like this

<select name="txtAvailable" rows="4" id="txtAvailable">
<Script Language = "vbscript" Runat = "Server">
For i = 0 to Count -1
Response.Write"<OPTION>" & ListOfFiles(i) & "</OPTION>"
next
</script></select>

I placed the vbscript in the approprate place in the body. But when i
open the page instead of writing the HTML where i placed the code, it
has appended it right at the bottom of the page. Does anyone know
where i'm going wrong or if there's a better way of doing this?

Thanks

Luke
Jul 19 '05 #1
10 1856
When things appear on the page where you don't expect them to, this is often
because of some poorly written HTML, not code. Example:

<table border="1">
<tr>
<td>XXX</td>
YYY
</tr>
</table>

The YYY will appear at the top of the page (in IE, anyway), although one may
expect it to appear in the table.

Basically, if you're seeing your select with the options in your array, your
ASP code isn't the problem.

Ray at work

"Lukelrc" <lu*********@westoxon.gov.uk> wrote in message
news:20**************************@posting.google.c om...
Hi,

I have an array - ListOfFiles - that i want use to populate an combo.

I've attempted to do it like this

<select name="txtAvailable" rows="4" id="txtAvailable">
<Script Language = "vbscript" Runat = "Server">
For i = 0 to Count -1
Response.Write"<OPTION>" & ListOfFiles(i) & "</OPTION>"
next
</script></select>

I placed the vbscript in the approprate place in the body. But when i
open the page instead of writing the HTML where i placed the code, it
has appended it right at the bottom of the page. Does anyone know
where i'm going wrong or if there's a better way of doing this?

Thanks

Luke

Jul 19 '05 #2
Lukelrc wrote:

<select name="txtAvailable" rows="4" id="txtAvailable">
<Script Language = "vbscript" Runat = "Server">
For i = 0 to Count -1
Response.Write"<OPTION>" & ListOfFiles(i) & "</OPTION>"
next
</script></select>

I placed the vbscript in the approprate place in the body. But when i
open the page instead of writing the HTML where i placed the code, it
has appended it right at the bottom of the page. Does anyone know
where i'm going wrong or if there's a better way of doing this?


This is an order-of-execution issue (http://aspfaq.com/show.asp?id=2045). If
you want to interlace server-side scripting with HTML, you will need <% %>
blocks.

I usually abstract as much processing as possible before writing any HTML.
In your case, I would build a single string to represent the entire array...

FilenameOptionList = CreateOptions(ListOfFiles,"")

....then insert it inline:

<SELECT><%=FilenameOptionList%></SELECT>

You can define the function anywhere in the script, either in <% %> blocks
or in <SCRIPT RUNAT="Server"> blocks. Here is a sample function:

================================================== ======================
Function CreateOptions(byVal A(), byVal DefaultValue)
Dim i, val
For i = 0 To UBound(A)
val = Server.HTMLEncode(A(i))
If A(i) = DefaultValue Then
A(i) = "<OPTION VALUE=""" & val & """ SELECTED>" & _
val & "</OPTION>"
Else
A(i) = "<OPTION VALUE=""" & val & """>" & _
val & "</OPTION>"
End If
Next
CreateOptions = Join(A,vbCrLf)
End Function
------------------------------------------------------------------------

Note that this example is capable of preselecting the control:

... = CreateOptions(ListOfFiles,Request.Form("txtAvailab le").Item)
Lastly, it bears mentioning that in many cases, this abstraction allows you
to simply use <SELECT><%=CreateOptions(...)%></SELECT>

More complicated functions can be written to create OPTION lists from 2D
arrays, wherein the .value and .text properties differ (<OPTION
VALUE="TN">Tennessee</OPTION>). Either way, this type of function is
entirely self-contained, so it can be placed into a common include for reuse
wherever needed.

If you use JScript on the server side, you can even do something like
this...

Array.prototype.toOptions = function(val) {
for (var i=0,a=[]; i<this.length; i++)
a[i] = "\r\n<OPTION VALUE=\"" + this[i] +
(this[i]==val ? "\" SELECTED>" : "\">") +
this[i] + "</OPTION>"
return a.join("")
}

....in which case:

<%=ListOfFiles.toOptions()%>

would suffice, but:

<%=ListOfFiles.toOptions(Request.Form("txtAvailabl e").Item)%>

works even better, and doesn't require a different method signature.
--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Jul 19 '05 #3
Ray at <%=sLocation%> [MVP] wrote:
When things appear on the page where you don't expect them to, this
is often because of some poorly written HTML, not code. Example:

<table border="1">
<tr>
<td>XXX</td>
YYY
</tr>
</table>

The YYY will appear at the top of the page (in IE, anyway), although
one may expect it to appear in the table.

Basically, if you're seeing your select with the options in your
array, your ASP code isn't the problem.


That's not correct, Ray. This is clearly an execution order problem. Note
the RUNAT="server" bit. If the default language is VBScript, that block
executes last, no matter where it sits in your code.

The fix is either abstraction or <%%> blocks.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Jul 19 '05 #4

"Dave Anderson" <GT**********@spammotel.com> wrote in message
news:ud*************@tk2msftngp13.phx.gbl...
Basically, if you're seeing your select with the options in your
array, your ASP code isn't the problem.


That's not correct, Ray. This is clearly an execution order problem. Note
the RUNAT="server" bit.


Oh yeah. Thanks Dave. Studying FAQ 2045 again now. :]

Ray at work
Jul 19 '05 #5
Thanks for all your help.

I have created a string as suggested FileNameoptionlist, which is fine.

Ive checked out about the execution order, but i cant see what exactly
the problem is. What is strange is that if i script:

<script language="vbscript"
Runat="server">Response.write(filenameOptionList)</script>

in either the head or the body, it produces the correct HTML but still
apended at the end - which does suggest an execution order problem, but
if i simply write:

<% response.write(Filenameoptionlist)%>

or

<% =filenameoptionlist%>

I get nothing at all!

Does anyone know why this might be?
Thanks in advance.

Luke

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 19 '05 #6
Thanks for all your help.

I have created a string as suggested FileNameoptionlist, which is fine.

Ive checked out about the execution order, but i cant see what exactly
the problem is. What is strange is that if i script:

<script language="vbscript"
Runat="server">Response.write(filenameOptionList)</script>

in either the head or the body, it produces the correct HTML but still
apended at the end - which does suggest an execution order problem, but
if i simply write:

<% response.write(Filenameoptionlist)%>

or

<% =filenameoptionlist%>

I get nothing at all!

Does anyone know why this might be?
Thanks in advance.

Luke
Jul 19 '05 #7
What about:

<% Response.WRite "Here is the filenameoptionslist: " & Filenameoptionlist
%>

Ray at work

"Lukelrc" <lu*********@westoxon.gov.uk> wrote in message
news:20**************************@posting.google.c om...
Thanks for all your help.

I have created a string as suggested FileNameoptionlist, which is fine.

Ive checked out about the execution order, but i cant see what exactly
the problem is. What is strange is that if i script:

<script language="vbscript"
Runat="server">Response.write(filenameOptionList)</script>

in either the head or the body, it produces the correct HTML but still
apended at the end - which does suggest an execution order problem, but
if i simply write:

<% response.write(Filenameoptionlist)%>

or

<% =filenameoptionlist%>

I get nothing at all!

Does anyone know why this might be?
Thanks in advance.

Luke

Jul 19 '05 #8
"Lukelrc" wrote in message
news:20**************************@posting.google.c om...
: I have an array - ListOfFiles - that i want use to populate an combo.
: I've attempted to do it like this
:
: <select name="txtAvailable" rows="4" id="txtAvailable">
: <Script Language = "vbscript" Runat = "Server">
: For i = 0 to Count -1
: Response.Write"<OPTION>" & ListOfFiles(i) & "</OPTION>"
: next
: </script></select>
:

Untested, just written here...

<%
Dim Count
Count = ubound(ListOfFiles())

sub prt(str)
Response.Write(str & vbCrLf)
end sub

sub lprt(str)
Response.Write(str & "<br />" & vbCrLf)
end sub

sub popList()
prt("<select name=""txtAvailable"" rows=""4"" id=""txtAvailable"">")
for i = 0 to Count - 1
prt("<option value=""" & ListOfFiles(i) & """>" & ListOfFiles(i) &
"</option>")
next
prt("</script>")
end sub

lprt("Check out my list of files...")
popList
%>

HTH...

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
Jul 19 '05 #9
I've sorted it. When i had written the script that populated the
string i had open and closed script tags several times. Putting all of
the code into one set of tags sorted it. Thanks for your help.

Luke
Jul 19 '05 #10
"Lukelrc" wrote in message
news:20**************************@posting.google.c om...
: I've sorted it. When i had written the script that populated the
: string i had open and closed script tags several times. Putting all of
: the code into one set of tags sorted it. Thanks for your help.

Glad to hear it's working for you Luke. (O:=
Jul 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: godber | last post by:
I need to populate text boxes for instance with employee information using their unique employee works number selected from a combo box. Can anyone help, I am told thru visual basic this can be...
4
by: Mike L | last post by:
I'm open for any suggestions on how to better program this. I want the user to select a license from a combo box, cboPrivilege and then the user will click the add button, then a record will be...
11
by: DSR | last post by:
Help Please... I would like to populate a combo box on a form with a query that compares data from two tables. Any record that is unique in table1 should continue to populate my combobox. The...
16
by: agrawal.solutions | last post by:
Hello Friends I am asking a very silly question but i dont find any solution fo this.. I am selectiong a recordset and want to populate a combobox where id would be inviseble and the content...
4
by: polaris431 | last post by:
All the examples I've seen showing how to populate a combobox using the DataSource property and an ArrayList show the ArrayList object containing objects with at least two properties. I want to...
1
by: indhu | last post by:
Hi all, I want 2 know, how to populate using 2 combo box to populate other field. right now am using click event of combo to populate the other field but i want to select both combo1 and combo2 ...
4
by: whamo | last post by:
I have the need to populate a field based on the selection in a combo box. Starting out simple. (2) tables tbl_OSE_Info and tbl_Input; tbl_OSE_Info has three fields: Key, OSE_Name and OSE_Wt...
5
by: giandeo | last post by:
Hello Experts. Could you find a solution for this problem please! I have the following tables in Access Database Table Name: origin Fields Names: country, countrycode Table Name: make...
4
by: =?Utf-8?B?R3JlZw==?= | last post by:
Can someone give me e simple example of to populate a combo box / list box using an ArrayList? THanks.
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.