使用数组填充组合 [英] Populate a combo with an array

查看:71
本文介绍了使用数组填充组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个数组 - ListOfFiles - 我想用它来填充一个组合。


我试过了这样做


< select name =" txtAvailable"行= QUOT; 4英寸id =" txtAvailable">

< Script Language =" vbscript" Runat =" Server">

for i = 0 to Count -1

Response.Write"< OPTION>" &安培; ListOfFiles(i)& "< / OPTION>"

next

< / script>< / select>


我放置了vbscript在身体的适当位置。但是当我打开页面而不是在我放置代码的地方写HTML时,

已经将它附加在页面底部。有谁知道

我哪里出错了,或者有更好的办法吗?


谢谢

Luke

解决方案

当页面上出现您不希望它们出现的情况时,这通常是

,因为写得不好,而不是代码。示例:


< table border =" 1">

< tr>

< td> XXX< ; / td>

YYY

< / tr>

< / table>


YYY将出现在页面顶部(在IE中,无论如何),尽管可能会有
期望它出现在表格中。


基本上,如果你看到你的数组中的选项你的选择,你的

ASP代码不是问题。


雷在工作


" Lukelrc" <卢********* @ westoxon.gov.uk>在消息中写道

news:20 ************************** @ posting.google.c om ...



我有一个数组 - ListOfFiles - 我想用它来填充一个组合。

我试图这样做像这样

< select name =" txtAvailable"行= QUOT; 4英寸id =" txtAvailable">
< Script Language =" vbscript" Runat =" Server">
对于i = 0到Count -1
Response.Write"< OPTION>" &安培; ListOfFiles(i)& "< / OPTION>"
下一页
< / script>< / select>

我将vbscript放在身体的适当位置。但是当我打开页面而不是编写放置代码的HTML时,它会将其附加在页面底部。有谁知道
我哪里出错或者有更好的办法吗?

谢谢

卢克



< select name =" txtAvailable"行= QUOT; 4英寸id =" txtAvailable">
< Script Language =" vbscript" Runat =" Server">
对于i = 0到Count -1
Response.Write"< OPTION>" &安培; ListOfFiles(i)& "< / OPTION>"
下一页
< / script>< / select>

我将vbscript放在身体的适当位置。但是当我打开页面而不是编写放置代码的HTML时,它会将其附加在页面底部。有谁知道
我哪里出错或是否有更好的方法这样做?




这是一个执行的命令问题( http://aspfaq.com/show.asp?id=2045 )。如果您希望使用HTML交换服务器端脚本,则需要< %%>

块。


我通常在编写任何HTML之前抽象尽可能多的处理。

在你的情况下,我会构建一个字符串来代表整个数组...


FilenameOptionList = CreateOptions(ListOfFiles,"")


....然后将其插入内联:


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


您可以在脚本中的任何位置定义函数,在< %%>中块

或< SCRIPT RUNAT =" Server">块。这是一个示例函数:


================================ ================== ======================

函数CreateOptions (byVal A(),byVal DefaultValue)

Dim i,val

For i = 0 To UBound(A)

val = Server。 HTMLEncode(A(i))

如果A(i)= DefaultValue那么

A(i)="< OPTION VALUE =""" &安培; val& """ SELECTED>" &安培; _

val& "< / OPTION>"

Else

A(i)="< OPTION VALUE =""" &安培; val& """>" &安培; _

val& "< / OPTION>"

结束如果

下一页

CreateOptions =加入(A,vbCrLf)

结束功能

-------------------------------------- ----------------------------------


请注意这个例子能够预先选择控件:


... = CreateOptions(ListOfFiles,Request.Form(" txtAvailab le))。Item)

最后,值得一提的是,在许多情况下,这种抽象允许你只需使用< SELECT><%= CreateOptions(...)%>< / SELECT>


可以编写更复杂的函数来从2D

数组创建OPTION列表,其中.value和.text属性不同(< OPTION

VALUE =" TN">田纳西< / OPTION>)。无论哪种方式,这种类型的功能都是完全独立的,所以它可以被放入一个通用的包含中以便重复使用



如果您在服务器端使用JScript,您甚至可以执行类似

这样的事情......


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>":" \">")+

这[i] +"< / OPTION>"

返回a.join("")

}


....在这种情况下:


<%= ListOfFiles.toOptions()%>


就足够了,但是:


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


工作得更好,并且不需要不同的方法签名。

-

戴夫安德森


将以每封邮件


500的费用阅读未经请求的商业电子邮件。使用

此电子邮件地址即表示同意这些条款。请不要直接联系

我或要求我直接与您联系以获取帮助。如果您的

问题值得询问,那就值得发帖。


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

解决方案

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



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.


这篇关于使用数组填充组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆