Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting the value from a listbox dtc

Status
Not open for further replies.

frogggg

Programmer
Jan 17, 2002
182
US
I have a listbox dtc which is populated from a recordset.
I want the user to select a value, the value to appear on a table together with some textboxes to be filled in, and then written to the database.
Two problems:
if I do listbox.getvalue it brings back the entire contents of the listbox all encrypted, instead of just the text that was selected. (I tried doing (recordset.fields.value("name")) with the recordset being the one that was writing to, not the one being populated from, but, which brings us to problem 2
how do i allow multiple select? I tried holding down ctrl but it didn't work. What do I have to put in the code?

I would appreciate any help greatly, especially with problem 1 because I have this on many pages.
 

Getting text from the Listbox DTC

listbox.getText() or listbox.getText(selectedIndex) - returns the selected text

listbox.getValue() - returns bound value, if there is one, if not, it's the same as getText

You can also get any other text from the listbox, not just the selected one:
listbox.getText(3)

I don't know about the multiple select. "Defeat is not the worst of failures. Not to have tried is the true failure."
-George E. Woodberry
 
Due to the interesting nature of script langauges, the syntax of
myDTClistbox.getValue
without () brackets will return the Code of the getValue function! (JavaScript - its in _scriptLibrary/LISTBOX.ASP).

As for Multi-Select lists, then you have 2 options. Firstly to change the Listbox javascript code [LISTBOX.ASP]. If you look at the display function:
function _LB_display(bReturnText)
you can see how it builds up the HTML for the listbox - and it does not currently include a MULTIPLE option. If you want this feature, then reply to this for more info.

The second option is to use normal HTML/ASP programming. The Browser returns any selected item values as a comma separated string for the Form. So for the list below, Request.Form("lstM") may return "A, B, C" if 3 items were selected. If you use numbers for your list values, then you must check the whole number, else value 1 will match the value 10 when using the instr() function - which is why I append a space and comma into the test. The recordset used in this code is a DTC recordset.

<select size=&quot;8&quot; id=&quot;lstM&quot; name=&quot;lstM&quot; MULTIPLE>
<%
dim strID
dim strSelected

strSelected = &quot; &quot; & Request.Form(&quot;lstM&quot;) & &quot;,&quot;

do while rsMRE.EOF = false
strID = rsMRE.fields.getValue(&quot;ID&quot;)
Response.Write &quot;<OPTION VALUE=&quot; & strID
'Is this selected?
if instr(strSelected, &quot; &quot; & strID & &quot;,&quot;) > 0 then
Response.Write &quot; SELECTED&quot;
end if
Response.Write &quot;>&quot; & _
rsMRE.fields.getValue(&quot;desciption&quot;) & _
&quot;</OPTION>&quot;
rsMRE.moveNext
loop
rsMRE.moveFirst
%>
</select>


(Content Management)
 
OK - I tried adding the parentheses, and I stopped getting the code. But I'm still not getting the selected value. I have:

response.write lstSkills.getText(selectedIndex)

and no go.

About the multiple select - I'm not quite sure I understand the html/asp part. Does this mean that you have 2 pages? an html and an asp? Because the listbox is populated from the database, and it needs to be .asp, from which it is impossible to submit a form.

Thanks for your help.
 
The
response.write lstSkills.getText(selectedIndex)
is almost right - but what does the 'selectedIndex' refer to?
By default the function will return the text of the selected item:
response.write lstSkills.getText()
should work
or you should qualify the 'selectedIndex' with its owner object...
response.write lstSkills.getText(lstSkills.selectedIndex)
If you needed to write Client Side code, it would be similar and you should always supply the required index value - like in the second example above.

The HTML/ASP thing is not two pages - just mixing plain HTML tags around a block of asp script <%...%>. In this case you start the list using html <SELECT...> then a block of script to generate the list items <OPTION...>, adding the 'SELECTED' option if the user has previously selected one or more entries. Then after the code block, its back to good old HTML.
(Content Management)
 
MerlinB, what else do you need to do the Listbox.asp to make it function with multiple selections? How would you modify the selectedIndex functionality?
 
devnc,

I got it working later by a completely different method. I'll post the code for you.

Here's how to do multiple select:
<script ID=&quot;clientEventHandlersJS&quot; LANGUAGE=&quot;javascript&quot;>
<!--
function window_onload() {
document.all.item(&quot;lstJobCategory&quot;).multiple = true;
}
//-->
</script>

That's all there is to it!

Here's how to fish the values out of the lisbox. You will need two pages. Page 1:

in an onClick event or such:
dim intCount
'dynamic array
dim arrayCategories()

'for each selected element in categories listbox
for intCount = 0 to Request.Form.Item(&quot;lstJobCategory&quot;).count - 1
'dynamically increase array size but preserve contents
redim preserve arrayCategories(intCount)
'populate array
arrayCategories(intCount) = Request.Form.Item(&quot;lstJobCategory&quot;)(intCount + 1)
next

'put array in session variable
session.Contents(&quot;CandidateCategories&quot;) = arrayCategories

Response.Redirect &quot;page2.asp&quot;

on Page2
dim intCount
'declare 2 arrays to hold field names and values
dim catFieldsArray(1)
dim catValuesArray(1)

for intCount = 0 to ubound(session.Contents(&quot;CandidateCategories&quot;))
response.write session.Contents(&quot;CandidateCategories&quot;)(intCount)
next

Hope that helps you!
 
You can easly add the MULTIPLE attribute to the HTML generated by the Listbox DTC (see the Display routine). However, the LISTBOX.ASP code assumes that only one item will be selected - so many methods and the selectedIndex variable would need to be altered to support multiple select during server round-trips (froggs method, while quite neat, does not support server round-trips). Its actually quite a bit of work, as follows:

in the _LB_display function:
1. MULTIPLE added to the SELECT tag
2. all selected items have the SELECTED attribute in their OPTION tag
various functions:
3. methods that set the selection to add to the existing selected list
4. (optional)new method needed to clear the selected list
5. methods that return the selected values or text should return a comma separated list or an array.
in the _LB_restoreState function:
6. the server round-trip support function needs to understand the multiple selection returned via the Request object. Froggs code could be used to set the selectedIndex with an array of values.
in the _LB__onrowenter/onbeforeupdate functions
7. the databinding routines need to set/return the selected list from the bound column - the column must support a comma separated list of the listbox values.

I suggest copying the LISTBOX.ASP to, say, MLISTBOX.ASP. Adjust the methods that use selectedIndex - so that selectedIndex becomes an array which you can add and remove index values. Methods like selectByValue should add to the selected list - and a new method (clearSelection) would be needed.
All internal methods should be prefixed with _MLB_ to avoid conflicts with the normal listbox _LB_ functions.
To create one on your page, just add a listbox DTC, get it working OK, then use the convert-to-runtime-text menu option. Then you can edit the LISTBOX.ASP reference and change it to MLISTBOX.ASP.

I hope this makes sense, and does not put you off having a go! (Content Management)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top