Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2242
  • Last Modified:

Displaying value/text of a listbox in vbscript / asp

I've got a piece of code (below) which will be used in an classic ASP VBScript to display a result based on the value in the listbox. The listbox is populated from an AD group and this bit works fine. However not having done much web development I'm struggling slightly on how I should display or capture the contents of the selected value in the listbox. Can someone help?

Thanks
Function GetAdmins(group)

Set objGroup = GetObject(group)
objGroup.getInfo
arrMemberOf = objGroup.GetEx("member")

Response.Write "<p>"
Response.Write "<font size=5 face=Verdana>"
Response.Write "<center><b>" & "ADMIN USERS" & "</br></br>"
Response.Write("</font>")
Response.Write("<select id=AdminList >")

	For Each strMember in arrMemberOf
		Set user = GetObject("LDAP://" & strMember)
		group = Replace(user.name,"CN=",vbNullString) '& "<br />" 
		Response.Write "<option value = '" & group & "'selected>"
		Response.Write group & "</Option>"

	Next

Response.Write "</SELECT>"
Response.Write "<input type=button name=SelectAdmin id=SelectAdmin value=RemoveUser />"
Response.Write "<script for=AdminList event=onChange language=vbscript>"
	
	msg = < ** CONTENTS OF LISTBOX HERE ** >
	Response.Write("MsgBox """ & msg & """<" & "/script>")

Response.Write "</script>"
Response.Write "</form>"
End Function

%>

Open in new window

0
bondy666
Asked:
bondy666
  • 7
  • 6
1 Solution
 
Pratima PharandeCommented:
you will get the value on post form by using

 <%=Request.Form ("AdminList")%>
0
 
bondy666Author Commented:
I have added the line:
 
msg = Request.Form("AdminList")

but when I run it I just get an empty msgbox.
0
 
RobSampsonCommented:
Have you tried:
MsgBox AdminList.Value
and
MsgBox AdminList.Text

That should work before a POST event, but when you submit the form, the Request method should work that was suggested.

Regards,

Rob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
bondy666Author Commented:
.value and .text were the first things I tried. It doesn't recognise .text at all (full vb only I think) and .value appears to be a keyword but it doesn't work in this instance, just get an error saying the page can't be displayed.
0
 
RobSampsonCommented:
I don't think you're allowed to have the extra Script tags in there.  Try this.

Regards,

Rob.
Function GetAdmins(group)
Set objGroup = GetObject(group)
objGroup.getInfo
arrMemberOf = objGroup.GetEx("member")

Response.Write "<p>"
Response.Write "<font size=5 face=Verdana>"
Response.Write "<center><b>" & "ADMIN USERS" & "</br></br>"
Response.Write "</font>"
Response.Write "<select id=AdminList >"
For Each strMember in arrMemberOf
	Set user = GetObject("LDAP://" & strMember)
	group = Replace(user.name, "CN=", vbNullString)
	Response.Write "<option value = '" & group & "' selected>"
	Response.Write group & "</Option>"
Next
Response.Write "</SELECT>"
Response.Write "<input type=button name=SelectAdmin id=SelectAdmin value=RemoveUser />"
Response.Write "</form>"

MsgBox AdminList.Value
End Function

Open in new window

0
 
RobSampsonCommented:
Or try this.  Create a span element on your page, like
<span id="MyForm"></span>

Regards,

Rob.
Function GetAdmins(group)
Set objGroup = GetObject(group)
objGroup.getInfo
arrMemberOf = objGroup.GetEx("member")

strHTML = "<p>"
strHTML = strHTML & VbCrLf & "<font size=5 face=Verdana>"
strHTML = strHTML & VbCrLf & "<center><b>" & "ADMIN USERS" & "</br></br>"
strHTML = strHTML & VbCrLf & "</font>"
strHTML = strHTML & VbCrLf & "<select id=AdminList >"
For Each strMember in arrMemberOf
	Set user = GetObject("LDAP://" & strMember)
	group = Replace(user.name, "CN=", vbNullString)
	strHTML = strHTML & VbCrLf & "<option value = '" & group & "' selected>"
	strHTML = strHTML & VbCrLf & group & "</Option>"
Next
strHTML = strHTML & VbCrLf & "</SELECT>"
strHTML = strHTML & VbCrLf & "<input type=button name=SelectAdmin id=SelectAdmin value=RemoveUser />"
strHTML = strHTML & VbCrLf & "</form>"

MyForm.InnerHTML = strHTML

MsgBox AdminList.Value
End Function

Open in new window

0
 
bondy666Author Commented:
Still no joy I'm afraid.
If it helps (which it might) I'm calling the code from another asp file. I have added your suggestion of the SPAN element to the calling file. Does this sound right? the content of the file is as follows:

<!--- #INCLUDE VIRTUAL = "/INCLUDES/Functions.asp" -->
<span id="MyForm"></span>
<%
 Call GetAdmins("LDAP://cn=CS-WorkFlowAdmins,OU=CS Groups,OU=CS,OU=HO,DC=net,DC=tiffani,DC=co,DC=uk")
%>

The main function is, as you might have guessed, in the functions.asp file.

Thanks
0
 
RobSampsonCommented:
Is it possible, just to test, for you to put the function code in the same ASP, with the SPAN object, just to see whether it works?  We might be best getting this to work in one ASP, then split it later.  I'll have to play around with a test server too.

Regards,

Rob.
0
 
bondy666Author Commented:
OK I've put it all in one function.
I've changed it slightly so a select case looks for a user called 'Lisa Smith' (who appears in the list) but she is ignored and the textbox will always say 'Someone Else' (because the AdminList doesn't pick up the text).
<span id="MyForm"></span>
<%
Call GetAdmins("LDAP://cn=CS-WorkFlowAdmins,OU=CS Groups,OU=CS,OU=HO,DC=net,DC=tiffani,DC=co,DC=uk")

Function GetAdmins(group)  
  
Set objGroup = GetObject(group)  
objGroup.getInfo  
arrMemberOf = objGroup.GetEx("member")  
Response.Write("<span id=MyForm></span>")
Response.Write "<p>"  
Response.Write "<font size=5 face=Verdana>"  
Response.Write "<center><b>" & "ADMIN USERS" & "</br></br>"  
Response.Write("</font>")  

Response.Write("<select name=AdminList id=AdminList>")  
  
        For Each strMember in arrMemberOf  
                Set user = GetObject("LDAP://" & strMember)  
                group = Replace(user.name,"CN=",vbNullString) '& "<br />"
				Response.Write (user.name)
                Response.Write "<OPTION VALUE=" & group & ">"  
                Response.Write group & "</OPTION>" 
				
	   Next  
  
Response.Write "</SELECT>"  
Response.Write "<input type=button name=SelectAdmin id=SelectAdmin value=RemoveUser />"  
Response.Write "<script for=AdminList event=onChange language=vbscript>"  
          
select case Request.form("AdminList")

	case "Lisa Smith"
		Response.Write("MsgBox """ & "Lisa" & """<" & "/script>")
	Case Else
		Response.Write("MsgBox """ & "Someone Else" & """<" & "/script>")

End Select
	  
Response.Write "</script>"  
Response.Write "</form>" 

End Function

Open in new window

0
 
bondy666Author Commented:
Sorry, slightly clearer:
 
<span id="MyForm"></span>
<%
Call GetAdmins("LDAP://cn=CS-WorkFlowAdmins,OU=CS Groups,OU=CS,OU=HO,DC=net,DC=tiffani,DC=co,DC=uk")

Function GetAdmins(group)  
  
Set objGroup = GetObject(group)  
objGroup.getInfo  
arrMemberOf = objGroup.GetEx("member")  
Response.Write("<span id=MyForm></span>")
Response.Write "<p>"  
Response.Write "<font size=5 face=Verdana>"  
Response.Write "<center><b>" & "ADMIN USERS" & "</br></br>"  
Response.Write("</font>")  

Response.Write("<select name=AdminList id=AdminList>")  
  
        For Each strMember in arrMemberOf  
                Set user = GetObject("LDAP://" & strMember)  
                group = Replace(user.name,"CN=",vbNullString)
                Response.Write "<OPTION VALUE=" & group & ">"  
                Response.Write group & "</OPTION>" 	
	    Next  
  
Response.Write "</SELECT>"  
Response.Write "<input type=button name=SelectAdmin id=SelectAdmin value=RemoveUser />"  
Response.Write "<script for=AdminList event=onChange language=vbscript>"  
          
select case Request.form("AdminList")

	case "Lisa Smith"
		Response.Write("MsgBox """ & "Lisa" & """<" & "/script>")
	Case Else
		Response.Write("MsgBox """ & "Someone Else" & """<" & "/script>")

End Select
	  
Response.Write "</script>"  
Response.Write "</form>" 

End Function

Open in new window

0
 
bondy666Author Commented:
Upped points
0
 
RobSampsonCommented:
OK, how about this?  This is the whole page, and you need to click the button after selecting a user.  It then performs a POST, and determines the name that was selected, and rebuilds the list box.

Regards,

Rob.
<%
If Request("RemoveUser") <> "" Then
	Response.Write "<BR>" & Request("AdminList") & " was selected from the list."
End If

Call GetAdmins("LDAP://cn=CS-WorkFlowAdmins,OU=CS Groups,OU=CS,OU=HO,DC=net,DC=tiffani,DC=co,DC=uk")

Function GetAdmins(group)  
	Set objGroup = GetObject(group)  
	objGroup.getInfo  
	arrMemberOf = objGroup.GetEx("member")  
	Response.Write "<form name=""MyForm"" action=""test.asp"" method=""post"">"
	Response.Write "<p>"  
	Response.Write "<font size=5 face=Verdana>"  
	Response.Write "<center><b>" & "ADMIN USERS" & "</br></br>"  
	Response.Write("</font>")  

	Response.Write("<select name=AdminList id=AdminList>")  
  
	For Each strMember in arrMemberOf  
		Set user = GetObject("LDAP://" & strMember)  
		group = Replace(user.name,"CN=",vbNullString)
		Response.Write "<OPTION VALUE=" & group & ">"  
		Response.Write group & "</OPTION>" 	
	Next  
  
	Response.Write "</SELECT>"  
	Response.Write "<input type=""submit"" name=""RemoveUser"" id=""RemoveUser"" value=""RemoveUser"" />"
	Response.Write "</form>"
End Function
%>
<html>
<body>
</body>
</html>

Open in new window

0
 
bondy666Author Commented:
Rob, you are indeed a scripting genius.
Can't believe how difficult an (apparently) simple task has been and you've saved me tearing my hair out for another week!
Excellent solution.
0
 
RobSampsonCommented:
Cool.  I'm glad that worked for you.  Thanks for the grade.

Regards,

Rob.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now