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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox - SELECT values according to variable

Status
Not open for further replies.

jasonhuibers

Programmer
Sep 12, 2005
290
CA
Listbox

strVar = 'John, Patrick'

How can we have the following listbox values SELECTED according to the values in the strVar using asp?

Tomas
John (This is selected)
Richard
Patrick (This is selected)
 
Give this a go
Code:
<%
function isSelected(strVar,strOpt)
	dim strArr : strArr = split(strVar,",")
	dim selected : selected=false
	isSelected=""
	if ubound(strArr) > 0 then
		for n = 0 to ubound(strArr)
			if lcase(trim(strArr(n))) = lcase(trim(strOpt)) then 
				selected = true
				exit for
			end if
		next
	else
		if lcase(trim(strVar)) = lcase(trim(strOpt)) then 
			selected = true
		end if
	end if
	if selected then
		isSelected = (" selected=""selected""")
	end if
end function

%>
<%strVar = "John, Patrick"%>
<select multiple size="5">
	<option value="Tomas" <%=isSelected(strVar,"Tomas")%>>Tomas</option>
	<option value="John"<%=isSelected(strVar,"John")%>>John</option>
	<option value="Richard"<%=isSelected(strVar,"Richard")%>>Richard</option>
	<option value="Patrick"<%=isSelected(strVar,"Patrick")%>>Patrick</option>
</select>

}...the bane of my life!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top