Solved

Displaying value/text of a listbox in vbscript / asp

Posted on 2011-03-03
14
2,101 Views
Last Modified: 2012-05-11
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
Comment
Question by:bondy666
  • 7
  • 6
14 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35026042
you will get the value on post form by using

 <%=Request.Form ("AdminList")%>
0
 

Author Comment

by:bondy666
ID: 35027127
I have added the line:
 
msg = Request.Form("AdminList")

but when I run it I just get an empty msgbox.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35032135
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:bondy666
ID: 35032389
.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
 
LVL 65

Expert Comment

by:RobSampson
ID: 35032599
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 35032611
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
 

Author Comment

by:bondy666
ID: 35034964
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 35045215
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
 

Author Comment

by:bondy666
ID: 35054959
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
 

Author Comment

by:bondy666
ID: 35054984
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
 

Author Comment

by:bondy666
ID: 35054991
Upped points
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35065941
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
 

Author Comment

by:bondy666
ID: 35068316
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 35073374
Cool.  I'm glad that worked for you.  Thanks for the grade.

Regards,

Rob.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

777 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