Bilgisayar Bölümü ASP Kod ve Script Arşivi

Birden fazla seçim yapılması

Dim intNumberSelected ' Count of items selected
Dim strSelectedTeams ' String returned from QS (Or Form)
Dim arrSelectedTeams ' Variable To hold team Array
Dim I ' Looping variable

' Retrieve the count of items selected
intNumberSelected = Request.Form(""teams"").Count

If intNumberSelected = 0 Then
Pick your favorite teams (hold down CTRL To Select more than one):<BR>

<FORM ACTION=""multiple_selection.asp"" METHOD=""post"">

The MULTIPLE parameter allows users To Select multiple items
from the pulldown box. The size attribute specifies how many
items tall To make the selection box.
<Select NAME=""teams"" MULTIPLE SIZE=""10"">
<Option>Anaheim Angels</Option>
<Option>Atlanta Braves</Option>
<Option>Arizona Diamondbacks</Option>
<Option>Baltimore Orioles</Option>
<Option>Boston Red Sox</Option>
<Option>Chicago Cubs</Option>
<Option>Chicago White Sox</Option>
<Option>Cincinnati Reds</Option>
<Option>Cleveland Indians</Option>
<Option>Colorado Rockies</Option>
<Option>Detroit Tigers</Option>
<Option>Florida Marlins</Option>
<Option>Houston Astros</Option>
<Option>Kansas City Royals</Option>
<Option>Los Angeles Dodgers</Option>
<Option>Milwaukee Brewers</Option>
<Option>Minnesota Twins</Option>
<Option>Montreal Expos</Option>
<Option>New York Mets</Option>
<Option>New York Yankees</Option>
<Option>Oakland Athletics</Option>
<Option>Philadelphia Phillies</Option>
<Option>Pittsburgh Pirates</Option>
<Option>San Diego Padres</Option>
<Option>San Francisco Giants</Option>
<Option>Seattle Mariners</Option>
<Option>St. Louis Cardinals</Option>
<Option>Tampa Bay Devil Rays</Option>
<Option>Texas Rangers</Option>
<Option>Toronto Blue Jays</Option>


<Input Type=""submit"" value=""Send Team Selection"">

' Retrieve the comma delimited list of teams that Is returned
' from the Form collection. This could also be gotten from
' the QueryString collection, but I used the post method
' instead of Get In my form.
strSelectedTeams = Request.Form(""teams"")

' Split our Text variable into an Array so we have easy
' programmatic access To the individual elements. Rememeber
' the Array will start at 0 Not 1 so a 10 item Array will
' run from 0 To 9 And Not 1 To 10!

' Split takes a String And Then searches For a delimiter
' (In this Case the comma followed by a space) In that String.
' It returns an Array of strings which consists of all the
' Text except the delimiters cut up into nice little pieces
' at the delimiters. The last two parameters specify the
' maximum number of delimiters To find (-1 = all) And the last
' one Is what Type of comparison To perform
' (0 = binary comparison, 1 = Text comparison)
arrSelectedTeams = Split(strSelectedTeams, "", "", -1, 1)

' One of our ever-vigilant visitors pointed out To Me that this
' will cause problems If any of your choices contain a comma.
' While he's right, I'm leaving the code As Is, because I feel
' exposing users To the split command And some Array work Is a
' good thing, but If you need To use commas try something Like
' this:
'ReDim arrSelectedTeams(intNumberSelected - 1)
'For I = 1 To intNumberSelected
' arrSelectedTeams(I - 1) = Request.Form(""teams"")(I)
'Next 'I
' We now join our regularly scheduled program already In progress...

' Show users the count of And String containing their choices
<P>You selected <B><%= intNumberSelected %></B> team(s).</P>

<P>Request.Form(""teams"") returned:</P>
<P><FONT SIZE=""-1""><B><%= strSelectedTeams %></B></FONT></P>

<P>You can easily convert this To an Array using the split command. The contents of that Array are shown In the table below:</P>

<TH>Array Element <FONT COLOR=""#FF0000"">*</FONT></TH>
' Some debugging lines If you start having problems
'Response.Write LBound(arrSelectedTeams)
'Response.Write UBound(arrSelectedTeams)

' Loop through the Array showing one table row For Each selection
For I = LBound(arrSelectedTeams) To UBound(arrSelectedTeams)
<TD><%= I %></TD>
<TD><%= arrSelectedTeams(I) %></TD>
Next 'I

<P><FONT COLOR=""#FF0000"">*</FONT> Remember that VBScript arrays start counting from 0. So a 10 item Array will run from 0 To 9!</P>

' Some code showing fully qualified requests. Might be fun To
' play With Or possible useful For debugging.
'Dim Item
'For Each Item In Request.Form
' Response.Write Request.Form.Key(Item) & "": ""
' Response.Write Request.Form.Item(Item) & "" ""
' Response.Write Request.Form.Item(Item).Count & ""<BR>""
End If

ASP Kitabı


Faydalılar: Altın Fiyatları |Hediye Kolye | pırlanta fiyatları
Ana Sayfa  Sık Kullanılanlara Ekle   admin[at] | © 2006 - 2023