Solved

Displaying value/text of a listbox in vbscript / asp

Posted on 2011-03-03
14
2,177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

615 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