Solved

Displaying value/text of a listbox in vbscript / asp

Posted on 2011-03-03
14
2,085 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now