Please Help with passing listbox <select multiple> values in asp vbscript 
Author Message
 Please Help with passing listbox <select multiple> values in asp vbscript

Hello George,
Generate Javascript code which will use the 'selected' property of
the Option object.  Here the sample code ...

   <html>
   <head>
    <title>Default Multiple Select Sample</title>
    <script language="Javascript" type="text/javascript">
    <!--
     function setDefaultSelection()
     {
      document.myform.fruits.options[0].selected = true;
      document.myform.fruits.options[2].selected = true;
      document.myform.fruits.options[3].selected = true;
     }
    //-->
    </script>
   </head>

   <body onload="setDefaultSelection()">

   <form name="myform">
   <select name="fruits" multiple size="5">
    <option value="1">Apple
    <option value="2">Grapes
    <option value="3">Orange
    <option value="4">Pineapple
    <option value="5">Banana
   </select>
   </form>

   </body>
   </html>

When the page is loaded, it choose the options "Apple", "Orange"
and "Pineapple" by default.  You will have to generate this code
from ASP dynamically for your purpose.

Arun Kumar K


Quote:
> Hi fellows,

> Please help me to figure out what is wrong with my script...
> My goal is to keep selected multiple choices from listbox in case if user
is
> transferred back (redirected) from the next page. I know how to accomplish
> it for dropdown menu, but with listbox I am kind of stuck. I am trying to
> use code below, but then my dynamically generated list values are
> disappearing on initial page load. If I to move first
> IF..Then..For..Next..Endif at the bottom of the page and use
response.write,
> I'll get my values displayed. So I know it is right way to go except my
> logic with If ..Then is broken :(

> Thanks in advance.
> George

>  <select name="Regions" size="5" multiple>

> <%
> While (NOT rsRegions.EOF)
> If Request.Form("Regions").count > 1 Then
>  For x=1 to Request.Form("Regions").count
>  If  CINT(rsRegions.Fields("RegionID")) =  Request.Form("Regions")(x) Then
> %>

> <option selected value="<%=(rsRegions.Fields.Item("RegionID").Value)%>"
> ><%=(rsRegions.Fields.Item("Description").Value)%></option>

> <% ELSE %>

> <option value="<%=(rsRegions.Fields.Item("RegionID").Value)%>"
> ><%=(rsRegions.Fields.Item("Description").Value)%></option>

>  <% End IF %>
>  <% Next %>
> <% End IF %>

> <%
>   rsRegions.MoveNext()
> Wend
> If (rsRegions.CursorType > 0) Then
>   rsRegions.MoveFirst
> Else
>   rsRegions.Requery
> End If
> %>
>         </select>



Fri, 27 Aug 2004 19:39:59 GMT  
 Please Help with passing listbox <select multiple> values in asp vbscript
Try changing the following line:

If  CINT(rsRegions.Fields("RegionID")) =  Request.Form("Regions")(x) Then

to:

If  CINT(rsRegions.Fields("RegionID")) =  CINT(Request.Form("Regions")(x))
Then

Also change:

rsRegions.MoveNext()

To rsRegions.MoveNext

Let us know if you require more help.

--Ryan N.

Quote:
> Hi fellows,

> Please help me to figure out what is wrong with my script...
> My goal is to keep selected multiple choices from listbox in case if user
is
> transferred back (redirected) from the next page. I know how to accomplish
> it for dropdown menu, but with listbox I am kind of stuck. I am trying to
> use code below, but then my dynamically generated list values are
> disappearing on initial page load. If I to move first
> IF..Then..For..Next..Endif at the bottom of the page and use
response.write,
> I'll get my values displayed. So I know it is right way to go except my
> logic with If ..Then is broken :(

> Thanks in advance.
> George

>  <select name="Regions" size="5" multiple>

> <%
> While (NOT rsRegions.EOF)
> If Request.Form("Regions").count > 1 Then
>  For x=1 to Request.Form("Regions").count
>  If  CINT(rsRegions.Fields("RegionID")) =  Request.Form("Regions")(x) Then
> %>

> <option selected value="<%=(rsRegions.Fields.Item("RegionID").Value)%>"
> ><%=(rsRegions.Fields.Item("Description").Value)%></option>

> <% ELSE %>

> <option value="<%=(rsRegions.Fields.Item("RegionID").Value)%>"
> ><%=(rsRegions.Fields.Item("Description").Value)%></option>

>  <% End IF %>
>  <% Next %>
> <% End IF %>

> <%
>   rsRegions.MoveNext()
> Wend
> If (rsRegions.CursorType > 0) Then
>   rsRegions.MoveFirst
> Else
>   rsRegions.Requery
> End If
> %>
>         </select>



Sat, 28 Aug 2004 03:28:43 GMT  
 Please Help with passing listbox <select multiple> values in asp vbscript
Thanks Ryan, but it did not help. Same result, my listbox is empty. As soon
as "For x=1 to Request.Form("Regions").count" comes into play values from
database are not coming through...

George


Quote:
> Try changing the following line:

> If  CINT(rsRegions.Fields("RegionID")) =  Request.Form("Regions")(x) Then

> to:

> If  CINT(rsRegions.Fields("RegionID")) =  CINT(Request.Form("Regions")(x))
> Then

> Also change:

> rsRegions.MoveNext()

> To rsRegions.MoveNext

> Let us know if you require more help.

> --Ryan N.


> > Hi fellows,

> > Please help me to figure out what is wrong with my script...
> > My goal is to keep selected multiple choices from listbox in case if
user
> is
> > transferred back (redirected) from the next page. I know how to
accomplish
> > it for dropdown menu, but with listbox I am kind of stuck. I am trying
to
> > use code below, but then my dynamically generated list values are
> > disappearing on initial page load. If I to move first
> > IF..Then..For..Next..Endif at the bottom of the page and use
> response.write,
> > I'll get my values displayed. So I know it is right way to go except my
> > logic with If ..Then is broken :(

> > Thanks in advance.
> > George

> >  <select name="Regions" size="5" multiple>

> > <%
> > While (NOT rsRegions.EOF)
> > If Request.Form("Regions").count > 1 Then
> >  For x=1 to Request.Form("Regions").count
> >  If  CINT(rsRegions.Fields("RegionID")) =  Request.Form("Regions")(x)
Then
> > %>

> > <option selected value="<%=(rsRegions.Fields.Item("RegionID").Value)%>"
> > ><%=(rsRegions.Fields.Item("Description").Value)%></option>

> > <% ELSE %>

> > <option value="<%=(rsRegions.Fields.Item("RegionID").Value)%>"
> > ><%=(rsRegions.Fields.Item("Description").Value)%></option>

> >  <% End IF %>
> >  <% Next %>
> > <% End IF %>

> > <%
> >   rsRegions.MoveNext()
> > Wend
> > If (rsRegions.CursorType > 0) Then
> >   rsRegions.MoveFirst
> > Else
> >   rsRegions.Requery
> > End If
> > %>
> >         </select>



Sat, 28 Aug 2004 04:35:47 GMT  
 Please Help with passing listbox <select multiple> values in asp vbscript
Replace your code example with the following:

<select name="Regions" size="5" multiple>

<%

Dim iIndex : iIndex=1

While NOT rsRegions.EOF
  If
CINT(rsRegions.Fields("RegionID"))=CINT(Request.Form("Regions")(iIndex))
Then
%>
  <option selected
value="<%=rsRegions.Fields("RegionID")%>"><%=rsRegions.Fields("Description")
%></option>

<%
  Else
%>
  <option
value="<%=rsRegions.Fields("RegionID")%>"><%=rsRegions.Fields("Description")
%></option>
<%
  End If

  rsRegions.MoveNext
  iIndex=iIndex+1
Wend

If rsRegions.CursorType > 0 Then
  rsRegions.MoveFirst
Else
  rsRegions.Requery
End If

%>
</select>

--Ryan N.

Quote:
> Thanks Ryan, but it did not help. Same result, my listbox is empty. As
soon
> as "For x=1 to Request.Form("Regions").count" comes into play values from
> database are not coming through...

> George



> > Try changing the following line:

> > If  CINT(rsRegions.Fields("RegionID")) =  Request.Form("Regions")(x)
Then

> > to:

> > If  CINT(rsRegions.Fields("RegionID")) =

CINT(Request.Form("Regions")(x))

- Show quoted text -

Quote:
> > Then

> > Also change:

> > rsRegions.MoveNext()

> > To rsRegions.MoveNext

> > Let us know if you require more help.

> > --Ryan N.


> > > Hi fellows,

> > > Please help me to figure out what is wrong with my script...
> > > My goal is to keep selected multiple choices from listbox in case if
> user
> > is
> > > transferred back (redirected) from the next page. I know how to
> accomplish
> > > it for dropdown menu, but with listbox I am kind of stuck. I am trying
> to
> > > use code below, but then my dynamically generated list values are
> > > disappearing on initial page load. If I to move first
> > > IF..Then..For..Next..Endif at the bottom of the page and use
> > response.write,
> > > I'll get my values displayed. So I know it is right way to go except
my
> > > logic with If ..Then is broken :(

> > > Thanks in advance.
> > > George

> > >  <select name="Regions" size="5" multiple>

> > > <%
> > > While (NOT rsRegions.EOF)
> > > If Request.Form("Regions").count > 1 Then
> > >  For x=1 to Request.Form("Regions").count
> > >  If  CINT(rsRegions.Fields("RegionID")) =  Request.Form("Regions")(x)
> Then
> > > %>

> > > <option selected

value="<%=(rsRegions.Fields.Item("RegionID").Value)%>"

- Show quoted text -

Quote:
> > > ><%=(rsRegions.Fields.Item("Description").Value)%></option>

> > > <% ELSE %>

> > > <option value="<%=(rsRegions.Fields.Item("RegionID").Value)%>"
> > > ><%=(rsRegions.Fields.Item("Description").Value)%></option>

> > >  <% End IF %>
> > >  <% Next %>
> > > <% End IF %>

> > > <%
> > >   rsRegions.MoveNext()
> > > Wend
> > > If (rsRegions.CursorType > 0) Then
> > >   rsRegions.MoveFirst
> > > Else
> > >   rsRegions.Requery
> > > End If
> > > %>
> > >         </select>



Sat, 28 Aug 2004 05:08:53 GMT  
 Please Help with passing listbox <select multiple> values in asp vbscript
Ryan, I am getting syntax error for

Quote:
>   If
> CINT(rsRegions.Fields("RegionID"))=CINT(Request.Form("Regions")(iIndex))
> Then

Placing them on single line:

Quote:
> If

CINT(rsRegions.Fields("RegionID"))=CINT(Request.Form("Regions")(iIndex))
Then

gives me an array error for same line number:

Request object, ASP 0105 (0x80004005)
An array index is out of range.

Any idea why?
Thanks
George


Quote:
> Replace your code example with the following:

> <select name="Regions" size="5" multiple>

> <%

> Dim iIndex : iIndex=1

> While NOT rsRegions.EOF
>   If
> CINT(rsRegions.Fields("RegionID"))=CINT(Request.Form("Regions")(iIndex))
> Then
> %>
>   <option selected

value="<%=rsRegions.Fields("RegionID")%>"><%=rsRegions.Fields("Description")
Quote:
> %></option>

> <%
>   Else
> %>
>   <option

value="<%=rsRegions.Fields("RegionID")%>"><%=rsRegions.Fields("Description")
Quote:
> %></option>
> <%
>   End If

>   rsRegions.MoveNext
>   iIndex=iIndex+1
> Wend

> If rsRegions.CursorType > 0 Then
>   rsRegions.MoveFirst
> Else
>   rsRegions.Requery
> End If

> %>
> </select>

> --Ryan N.


> > Thanks Ryan, but it did not help. Same result, my listbox is empty. As
> soon
> > as "For x=1 to Request.Form("Regions").count" comes into play values
from
> > database are not coming through...

> > George



> > > Try changing the following line:

> > > If  CINT(rsRegions.Fields("RegionID")) =  Request.Form("Regions")(x)
> Then

> > > to:

> > > If  CINT(rsRegions.Fields("RegionID")) =
> CINT(Request.Form("Regions")(x))
> > > Then

> > > Also change:

> > > rsRegions.MoveNext()

> > > To rsRegions.MoveNext

> > > Let us know if you require more help.

> > > --Ryan N.


> > > > Hi fellows,

> > > > Please help me to figure out what is wrong with my script...
> > > > My goal is to keep selected multiple choices from listbox in case if
> > user
> > > is
> > > > transferred back (redirected) from the next page. I know how to
> > accomplish
> > > > it for dropdown menu, but with listbox I am kind of stuck. I am
trying
> > to
> > > > use code below, but then my dynamically generated list values are
> > > > disappearing on initial page load. If I to move first
> > > > IF..Then..For..Next..Endif at the bottom of the page and use
> > > response.write,
> > > > I'll get my values displayed. So I know it is right way to go except
> my
> > > > logic with If ..Then is broken :(

> > > > Thanks in advance.
> > > > George

> > > >  <select name="Regions" size="5" multiple>

> > > > <%
> > > > While (NOT rsRegions.EOF)
> > > > If Request.Form("Regions").count > 1 Then
> > > >  For x=1 to Request.Form("Regions").count
> > > >  If  CINT(rsRegions.Fields("RegionID")) =

Request.Form("Regions")(x)

- Show quoted text -

Quote:
> > Then
> > > > %>

> > > > <option selected
> value="<%=(rsRegions.Fields.Item("RegionID").Value)%>"
> > > > ><%=(rsRegions.Fields.Item("Description").Value)%></option>

> > > > <% ELSE %>

> > > > <option value="<%=(rsRegions.Fields.Item("RegionID").Value)%>"
> > > > ><%=(rsRegions.Fields.Item("Description").Value)%></option>

> > > >  <% End IF %>
> > > >  <% Next %>
> > > > <% End IF %>

> > > > <%
> > > >   rsRegions.MoveNext()
> > > > Wend
> > > > If (rsRegions.CursorType > 0) Then
> > > >   rsRegions.MoveFirst
> > > > Else
> > > >   rsRegions.Requery
> > > > End If
> > > > %>
> > > >         </select>



Sat, 28 Aug 2004 05:53:21 GMT  
 Please Help with passing listbox <select multiple> values in asp vbscript
Will you post the entire page as an attachment along with a description of
the fields in the database?  I am referring to the page that builds the
dynamic list and the one presents the originally selected values to the
user.

--Ryan N.


Quote:
> This one did not get an error and values are populated from database,  but
> now I am back to the beginning: originally selected values do not stay
> selected upon server.transfer back to this page...

> If that's is any help, here is how I am creating that recordset:
> <%
> set rsRegions = Server.CreateObject("ADODB.Recordset")
> rsRegions.ActiveConnection = MY_DBOCONNECTION_STRING    ' from include
file
> rsRegions.Source = "SELECT RegionID, Description FROM dbo.Regions ORDER BY
> RegionID ASC"
> rsRegions.CursorType = 0
> rsRegions.CursorLocation = 2
> rsRegions.LockType = 3
> rsRegions.Open()
> rsRegions_numRows = 0
> %>

> I am a little lost now... If my values in the listbox created dynamically
by
> calling recordset, then how can be
> >  more records in the recordset than items in the form collection?

> or I am missing something in your explanation?

> Thanks anyway for helping me out and your time.
> George



> > hmmm...you are receiving that error because there are more records in
the
> > recordset than items in the form collection.  Attached is a modified-but
> not
> > tested-version of the example code that checks whether the iIndex
variable
> > is less than or equal to the form collection count before trying to
access
> > an item in the collection.

> > Give it a whirl and let us know if you require further assistance.

> > --Ryan N.



> > > Got your code, thanks. But same array index error. I am running IIS
5.1
> > with
> > > msde 2000.

> > > George



> > > > Darn the word e-mail client word wrap.  I have attached the sample
> code
> > > for
> > > > you-hope your NNTP client supports attachments and the formatting
> > doesn't
> > > > change.

> > > > What version of IIS are you running?

> > > > --Ryan N.


> > > > > Ryan, I am getting syntax error for

> > > > > >   If

CINT(rsRegions.Fields("RegionID"))=CINT(Request.Form("Regions")(iIndex))

- Show quoted text -

Quote:
> > > > > > Then

> > > > > Placing them on single line:

> > > > > > If

> > CINT(rsRegions.Fields("RegionID"))=CINT(Request.Form("Regions")(iIndex))
> > > > > Then

> > > > > gives me an array error for same line number:

> > > > > Request object, ASP 0105 (0x80004005)
> > > > > An array index is out of range.

> > > > > Any idea why?
> > > > > Thanks
> > > > > George



> > > > > > Replace your code example with the following:

> > > > > > <select name="Regions" size="5" multiple>

> > > > > > <%

> > > > > > Dim iIndex : iIndex=1

> > > > > > While NOT rsRegions.EOF
> > > > > >   If

CINT(rsRegions.Fields("RegionID"))=CINT(Request.Form("Regions")(iIndex))
Quote:
> > > > > > Then
> > > > > > %>
> > > > > >   <option selected

value="<%=rsRegions.Fields("RegionID")%>"><%=rsRegions.Fields("Description")
Quote:
> > > > > > %></option>

> > > > > > <%
> > > > > >   Else
> > > > > > %>
> > > > > >   <option

value="<%=rsRegions.Fields("RegionID")%>"><%=rsRegions.Fields("Description")

- Show quoted text -

Quote:
> > > > > > %></option>
> > > > > > <%
> > > > > >   End If

> > > > > >   rsRegions.MoveNext
> > > > > >   iIndex=iIndex+1
> > > > > > Wend

> > > > > > If rsRegions.CursorType > 0 Then
> > > > > >   rsRegions.MoveFirst
> > > > > > Else
> > > > > >   rsRegions.Requery
> > > > > > End If

> > > > > > %>
> > > > > > </select>

> > > > > > --Ryan N.


> > > > > > > Thanks Ryan, but it did not help. Same result, my listbox is
> > empty.
> > > As
> > > > > > soon
> > > > > > > as "For x=1 to Request.Form("Regions").count" comes into play
> > values
> > > > > from
> > > > > > > database are not coming through...

> > > > > > > George



> > > > > > > > Try changing the following line:

> > > > > > > > If  CINT(rsRegions.Fields("RegionID")) =
> > > Request.Form("Regions")(x)
> > > > > > Then

> > > > > > > > to:

> > > > > > > > If  CINT(rsRegions.Fields("RegionID")) =
> > > > > > CINT(Request.Form("Regions")(x))
> > > > > > > > Then

> > > > > > > > Also change:

> > > > > > > > rsRegions.MoveNext()

> > > > > > > > To rsRegions.MoveNext

> > > > > > > > Let us know if you require more help.

> > > > > > > > --Ryan N.


> > > > > > > > > Hi fellows,

> > > > > > > > > Please help me to figure out what is wrong with my
script...
> > > > > > > > > My goal is to keep selected multiple choices from listbox
in
> > > case
> > > > if
> > > > > > > user
> > > > > > > > is
> > > > > > > > > transferred back (redirected) from the next page. I know
how
> > to
> > > > > > > accomplish
> > > > > > > > > it for dropdown menu, but with listbox I am kind of stuck.
I
> > am
> > > > > trying
> > > > > > > to
> > > > > > > > > use code below, but then my dynamically generated list
> values
> > > are
> > > > > > > > > disappearing on initial page load. If I to move first
> > > > > > > > > IF..Then..For..Next..Endif at the bottom of the page and
use
> > > > > > > > response.write,
> > > > > > > > > I'll get my values displayed. So I know it is right way to
> go
> > > > except
> > > > > > my
> > > > > > > > > logic with If ..Then is broken :(

> > > > > > > > > Thanks in advance.
> > > > > > > > > George

> > > > > > > > >  <select name="Regions" size="5" multiple>

> > > > > > > > > <%
> > > > > > > > > While (NOT rsRegions.EOF)
> > > > > > > > > If Request.Form("Regions").count > 1 Then
> > > > > > > > >  For x=1 to Request.Form("Regions").count
> > > > > > > > >  If  CINT(rsRegions.Fields("RegionID")) =
> > > > > Request.Form("Regions")(x)
> > > > > > > Then
> > > > > > > > > %>

> > > > > > > > > <option selected
> > > > > > value="<%=(rsRegions.Fields.Item("RegionID").Value)%>"

><%=(rsRegions.Fields.Item("Description").Value)%></option>

> > > > > > > > > <% ELSE %>

> > > > > > > > > <option
> value="<%=(rsRegions.Fields.Item("RegionID").Value)%>"

><%=(rsRegions.Fields.Item("Description").Value)%></option>

> > > > > > > > >  <% End IF %>
> > > > > > > > >  <% Next %>
> > > > > > > > > <% End IF %>

> > > > > > > > > <%
> > > > > > > > >   rsRegions.MoveNext()
> > > > > > > > > Wend
> > > > > > > > > If (rsRegions.CursorType > 0) Then
> > > > > > > > >   rsRegions.MoveFirst
> > > > > > > > > Else
> > > > > > > > >   rsRegions.Requery
> > > > > > > > > End If
> > > > > > > > > %>
> > > > > > > > >         </select>



Sat, 28 Aug 2004 09:09:18 GMT  
 Please Help with passing listbox <select multiple> values in asp vbscript
Phew...I was beginning to get nervous...I thought you didn't have any items
in the Form collection.

I'll look over this and post a response in the morning.

--Ryan N.



Sat, 28 Aug 2004 14:22:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Please Help - Passing SELECT values to ASP

2. <<<<<<<<ComboBox>>>>>>>>>>>>

3. HELP >>>>>>WIN API <<<<<<<<<<

4. Help: Data Binding a <SELECT> Listbox

5. --------<<<<<<MONEY>>>>>>--------- id 9543

6. VBScript + Forms + <Select Multiple>

7. >>>>> URGENT: PLEASE HELP <<<<<

8. Help recasting UDT <<PLEASE!>>

9. help me out please!!!!<<<<<<<<<<<<<<<<<<<<<<<thanks>>>>>>>>>>>>>>>>>>>>>>>>

10. Main report value<-pass/return->subReport values (ActiveX Designer Component 6.0)

11. <><><>HELP<><><> PCMCIA Motorola Montana 33.6

12. >>>>HELP ME WITH DATEVALUE IN UK FORMAT<<<<<<<

 

 
Powered by phpBB® Forum Software