473,748 Members | 6,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="txtAvaila ble" rows="4" id="txtAvailabl e">
<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 1873
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*********@we stoxon.gov.uk> wrote in message
news:20******** *************** ***@posting.goo gle.com...
Hi,

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

I've attempted to do it like this

<select name="txtAvaila ble" rows="4" id="txtAvailabl e">
<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="txtAvaila ble" rows="4" id="txtAvailabl e">
<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...

FilenameOptionL ist = CreateOptions(L istOfFiles,"")

....then insert it inline:

<SELECT><%=File nameOptionList% ></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(b yVal A(), byVal DefaultValue)
Dim i, val
For i = 0 To UBound(A)
val = Server.HTMLEnco de(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(L istOfFiles,Requ est.Form("txtAv ailable").Item)
Lastly, it bears mentioning that in many cases, this abstraction allows you
to simply use <SELECT><%=Crea teOptions(...)% ></SELECT>

More complicated functions can be written to create OPTION lists from 2D
arrays, wherein the .value and .text properties differ (<OPTION
VALUE="TN">Tenn essee</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<OPTIO N VALUE=\"" + this[i] +
(this[i]==val ? "\" SELECTED>" : "\">") +
this[i] + "</OPTION>"
return a.join("")
}

....in which case:

<%=ListOfFiles. toOptions()%>

would suffice, but:

<%=ListOfFiles. toOptions(Reque st.Form("txtAva ilable").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**********@s pammotel.com> wrote in message
news:ud******** *****@tk2msftng p13.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 FileNameoptionl ist, 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="vbscr ipt"
Runat="server"> Response.write( filenameOptionL ist)</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( Filenameoptionl ist)%>

or

<% =filenameoption list%>

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 FileNameoptionl ist, 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="vbscr ipt"
Runat="server"> Response.write( filenameOptionL ist)</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( Filenameoptionl ist)%>

or

<% =filenameoption list%>

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 filenameoptions list: " & Filenameoptionl ist
%>

Ray at work

"Lukelrc" <lu*********@we stoxon.gov.uk> wrote in message
news:20******** *************** ***@posting.goo gle.com...
Thanks for all your help.

I have created a string as suggested FileNameoptionl ist, 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="vbscr ipt"
Runat="server"> Response.write( filenameOptionL ist)</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( Filenameoptionl ist)%>

or

<% =filenameoption list%>

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.goo gle.com...
: I have an array - ListOfFiles - that i want use to populate an combo.
: I've attempted to do it like this
:
: <select name="txtAvaila ble" rows="4" id="txtAvailabl e">
: <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(ListOfFi les())

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

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

sub popList()
prt("<select name=""txtAvail able"" rows=""4"" id=""txtAvailab le"">")
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

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

Similar topics

4
12847
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 achieved.
4
3006
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 added to the data grid. My current code is coming up with errors, dt and drv are unknown in the else part of the if statement. if (this.dgPrivileges.DataSource == null) {
11
12309
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 instant the record appears also in table2 it should no longer be listed in my combo box. This is not a key field in either of the tables. Basically what I am trying to do is prevent the user from entering duplicate values even though it is not a key...
16
10530
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 would. I am getting the recordset and the no of record but then i am unble to populate the combobox. I have already tried all the function starting form for each x in .... and while...wend and do....loop
4
28228
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 create an ArrayList that contains only rows of string data and want to assign this to the DataSource but I can't figure out what to set the ValueMember or DisplayMember to. Example ArrayList myData = new ArrayList(); myData.Add("abc");
1
1598
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 when these 2 r equal then it populate other fields. Private Sub scene_txt_Click() 'combo name scene_Txt
4
13970
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 tbl_Input has three fields: OSE_Job, OSE_Name, OSE_Wt I have populated tbl_OSE_Info table. I need to create a form that will store the data in tbl_Input I have racked my brain so much trying to figure out how to auto populate a field based on a...
5
4007
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 Fields Names: countrycode, make
4
9249
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.
0
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9325
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.