Solved

Displaying value/text of a listbox in vbscript / asp

Posted on 2011-03-03
14
2,071 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 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

19 Experts available now in Live!

Get 1:1 Help Now