Solved

ASP Multiple Select List Box

Posted on 2011-09-08
11
373 Views
Last Modified: 2012-05-12
Hello,

I am using an HTML Listbox control in my ASP page. I am using it with "Allow
Multiple Selections" option set to true.

Now on my EDIT page I am unsure how to within a listbox display all of the options that have been selected?

At present my code just selects the first?

Here it is -

<select name="category" size="8" multiple="multiple" id="category">
  <option value="" <%If (Not isNull((RsSelectedCategories.Fields.Item("ID").Value))) Then If ("" = CStr((RsSelectedCategories.Fields.Item("ID").Value))) Then Response.Write("selected=""selected""") : Response.Write("")%>></option>
  <%
While (NOT RsCategoriesOptions.EOF)
%>
  <option value="<%=(RsCategoriesOptions.Fields.Item("ID").Value)%>" <%If (Not isNull((RsSelectedCategories.Fields.Item("ID").Value))) Then If (CStr(RsCategoriesOptions.Fields.Item("ID").Value) = CStr((RsSelectedCategories.Fields.Item("ID").Value))) Then Response.Write("selected=""selected""") : Response.Write("")%> ><%=(RsCategoriesOptions.Fields.Item("Category-label").Value)%></option>
  <%
  RsCategoriesOptions.MoveNext()
Wend
If (RsCategoriesOptions.CursorType > 0) Then
  RsCategoriesOptions.MoveFirst
Else
  RsCategoriesOptions.Requery
End If
%>

Do i need extra loop in here?

Thank you
</select>

Open in new window

0
Comment
Question by:garethtnash
  • 7
  • 4
11 Comments
 
LVL 15

Expert Comment

by:pateljitu
ID: 36503185
Please try this, it is not the most elegant way though:

<select name="category" size="8" multiple="multiple" id="category">
  <option value="" <%If (Not isNull((RsSelectedCategories.Fields.Item("ID").Value))) Then If ("" = CStr((RsSelectedCategories.Fields.Item("ID").Value))) Then Response.Write("selected=""selected""") : Response.Write("")%>></option>
  <%
While (NOT RsCategoriesOptions.EOF)

    While (NOT RsSelectedCategories.EOF)
%>
  <option value="<%=(RsCategoriesOptions.Fields.Item("ID").Value)%>" <%If (Not isNull((RsSelectedCategories.Fields.Item("ID").Value))) Then If (CStr(RsCategoriesOptions.Fields.Item("ID").Value) = CStr((RsSelectedCategories.Fields.Item("ID").Value))) Then Response.Write("selected=""selected""") : Response.Write("")%> ><%=(RsCategoriesOptions.Fields.Item("Category-label").Value)%></option>
  <%
    RsSelectedCategories.MoveNext()
  wend
 
  RsCategoriesOptions.MoveNext()
Wend

If (RsCategoriesOptions.CursorType > 0) Then
  RsCategoriesOptions.MoveFirst
Else
  RsCategoriesOptions.Requery
End If
%>
0
 

Author Comment

by:garethtnash
ID: 36503251
Hi Pateljitu, sorry no joy, it just repeats the

RsSelectedCategories Option  three times (I know that there are 3 records here)

I can't see the other

RsCategoriesOptions Options;

For clarity

RsCategoriesOptions Options; are the original options
RsSelectedCategories are the selected options

Thanks in advance -

 
0
 
LVL 15

Expert Comment

by:pateljitu
ID: 36503348
Please try this:

<select name="category" size="8" multiple="multiple" id="category">
  <option value="" <%If (Not isNull((RsSelectedCategories.Fields.Item("ID").Value))) Then If ("" = CStr((RsSelectedCategories.Fields.Item("ID").Value))) Then Response.Write("selected=""selected""") : Response.Write("")%>></option>
  <%
While (NOT RsCategoriesOptions.EOF)
%>
    <option value="<%=(RsCategoriesOptions.Fields.Item("ID").Value)%>"
<%    
        do While NOT RsSelectedCategories.EOF

            If (Not isNull((RsSelectedCategories.Fields.Item("ID").Value))) Then
                If (CStr(RsCategoriesOptions.Fields.Item("ID").Value) = CStr((RsSelectedCategories.Fields.Item("ID").Value))) Then
                    Response.Write("selected=""selected""")
                    exit do
                end if
            End if        
     
        RsSelectedCategories.MoveNext()
        loop
 %>
    ><%=(RsCategoriesOptions.Fields.Item("Category-label").Value)%></option>
 <%
 
  RsCategoriesOptions.MoveNext()
Wend

If (RsCategoriesOptions.CursorType > 0) Then
  RsCategoriesOptions.MoveFirst
Else
  RsCategoriesOptions.Requery
End If
%>
0
 

Author Comment

by:garethtnash
ID: 36503370
Close,

that gives me all of the products that were previously selected, butt none of the other options?

Almost there - I think?
0
 
LVL 15

Expert Comment

by:pateljitu
ID: 36503652
You mean not all records from recordset "RsCategoriesOptions" are getting displayed.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:garethtnash
ID: 36503663
thats correct
0
 

Author Comment

by:garethtnash
ID: 36503666
none of the unselected ones
0
 
LVL 15

Accepted Solution

by:
pateljitu earned 500 total points
ID: 36503993
Should work, try this also if there is issue with this code comment line "exit do" and try again:


<select name="category" size="8" multiple="multiple" id="category">
  <option value="" <%If (Not isNull((RsSelectedCategories.Fields.Item("ID").Value))) Then If ("" = CStr((RsSelectedCategories.Fields.Item("ID").Value))) Then Response.Write("selected=""selected""") : Response.Write("")%>></option>
  <%
While (NOT RsCategoriesOptions.EOF)
%>
    <option value="<%=(RsCategoriesOptions.Fields.Item("ID").Value)%>"
<%    
        do While NOT RsSelectedCategories.EOF

            If (Not isNull((RsSelectedCategories.Fields.Item("ID").Value))) Then
                If (CStr(RsCategoriesOptions.Fields.Item("ID").Value) = CStr((RsSelectedCategories.Fields.Item("ID").Value))) Then
                    Response.Write(" selected=""selected"" ")
                    exit do
                end if
            End if        
     
        RsSelectedCategories.MoveNext()
        loop
 %>
    ><%=(RsCategoriesOptions.Fields.Item("Category-label").Value)%></option>
 <%
 
  RsCategoriesOptions.MoveNext()
Wend

'If (RsCategoriesOptions.CursorType > 0) Then
'  RsCategoriesOptions.MoveFirst
'Else
'  RsCategoriesOptions.Requery
'End If
%>
</select>
0
 

Author Comment

by:garethtnash
ID: 36509102
Hi Pateljitu,

That is fantastic thank you - the following works a treat --

<select name="category" size="4" multiple="multiple" id="category">
  <%
While (NOT RsCategoriesOptions.EOF)
%>
    <option value="<%=(RsCategoriesOptions.Fields.Item("ID").Value)%>"
<%    
        do While NOT RsSelectedCategories.EOF

            If (Not isNull((RsSelectedCategories.Fields.Item("ID").Value))) Then
                If (CStr(RsCategoriesOptions.Fields.Item("ID").Value) = CStr((RsSelectedCategories.Fields.Item("ID").Value))) Then
                    Response.Write(" selected=""selected"" ")
                    exit do
                end if
            End if        
     
        RsSelectedCategories.MoveNext()
        loop
 %>
    ><%=(RsCategoriesOptions.Fields.Item("Category-label").Value)%></option>
 <%
 
  RsCategoriesOptions.MoveNext()
Wend
%>
</select>

Open in new window


Thank you
0
 

Author Closing Comment

by:garethtnash
ID: 36509103
First Class - thank you
0
 

Author Comment

by:garethtnash
ID: 36522116
Hi Pateljitu,

Sorry to trouble you, I've tried using your logic on another select box with multiple options on the page but to no joy, would you mind having a look please --

<%
Dim RsRelatedProductOptions__MMColParam
RsRelatedProductOptions__MMColParam = "0"
If (Request("ID") <> "") Then 
  RsRelatedProductOptions__MMColParam = Request("ID")
End If
%>
<%
Dim RsRelatedProductOptions
Dim RsRelatedProductOptions_cmd
Dim RsRelatedProductOptions_numRows

Set RsRelatedProductOptions_cmd = Server.CreateObject ("ADODB.Command")
RsRelatedProductOptions_cmd.ActiveConnection = MM_GolfConnection_STRING
RsRelatedProductOptions_cmd.CommandText = "SELECT ID, [Product-Name] FROM dbo.[Direct-Product] WHERE [Display-TGI-Direct] = 'Y' AND ID <> ? ORDER BY [Product-Name]" 
RsRelatedProductOptions_cmd.Prepared = true
RsRelatedProductOptions_cmd.Parameters.Append RsRelatedProductOptions_cmd.CreateParameter("param1", 5, 1, -1, RsRelatedProductOptions__MMColParam) ' adDouble

Set RsRelatedProductOptions = RsRelatedProductOptions_cmd.Execute
RsRelatedProductOptions_numRows = 0
%>
<%
Dim RsRelated__MMColParam
RsRelated__MMColParam = "0"
If (Request("ID") <> "") Then 
  RsRelated__MMColParam = Request("ID")
End If
%>
<%
Dim RsRelated
Dim RsRelated_cmd
Dim RsRelated_numRows

Set RsRelated_cmd = Server.CreateObject ("ADODB.Command")
RsRelated_cmd.ActiveConnection = MM_GolfConnection_STRING
RsRelated_cmd.CommandText = "Select  R.RelatedProductID as ID from dbo. [Direct-Related-Products] R  where R.ProductID = ?" 
RsRelated_cmd.Prepared = true
RsRelated_cmd.Parameters.Append RsRelated_cmd.CreateParameter("param1", 5, 1, -1, RsRelated__MMColParam) ' adDouble

Set RsRelated = RsRelated_cmd.Execute
RsRelated_numRows = 0
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<select name="category" size="4" multiple="multiple" id="category">
  <%
While (NOT RsRelatedProductOptions.EOF)
%>
    <option value="<%=(RsRelatedProductOptions.Fields.Item("ID").Value)%>"
<%    
        do While NOT RsRelated.EOF

            If (Not isNull((RsRelated.Fields.Item("ID").Value))) Then
                If (CStr(RsRelatedProductOptions.Fields.Item("ID").Value) = CStr((RsRelated.Fields.Item("ID").Value))) Then
                    Response.Write(" selected=""selected"" ")
                    exit do
                end if
            End if        
     
        RsRelated.MoveNext()
        loop
 %>
    ><%=(RsRelatedProductOptions.Fields.Item("Product-Name").Value)%></option>
 <%
 
  RsRelatedProductOptions.MoveNext()
Wend
%>
</select>
</body>
</html>
<%
RsRelatedProductOptions.Close()
Set RsRelatedProductOptions = Nothing
%>
<%
RsRelated.Close()
Set RsRelated = Nothing
%>

Open in new window


The page in question is here

For the ID value 39 --

this ;

Select  R.RelatedProductID as ID from dbo. [Direct-Related-Products] R  where R.ProductID = 39

Produces the following results -

ID
16
17
18
19

this; SELECT ID, [Product-Name] FROM dbo.[Direct-Product] WHERE [Display-TGI-Direct] = 'Y' AND ID <> 39 ORDER BY [Product-Name]

Produces the following results -

ID      Product-Name
40      final Test Hopefully &#163;&#163;$
16      Product1
17      Product2
18      Product3
19      Product4

So Products 1 - 4 should be highlighted, but are not...

Please advise.

Many thanks



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now