Web Connection
Retrieving the selected value from an HTMLDropDown list
Gravatar is a globally recognized avatar based on your email address. Retrieving the selected value from an HTMLDropDown list
  Serge
  All
  Nov 30, 2019 @ 06:02pm

I am sure this is something simple, but can someone show me how we return the selected value from a HTMLDropDown?

In my Process class I have defined the attribute for the selected variable and I have generated the cursor that populates the list;

Function cdep_2()
	#If .F.
		Local Request As wwrequest, Response As wwpageresponse, Process As wwprocess
	#Endif
	Local lc_SelDept As Character
	Local lc_DeptOut As Character
	Store "001" to lc_SelDept

	If !Request.IsPostBack()
		sele * from myTable into cursor cDept
	endif
	Response.ExpandTemplate()
endfunc

In my HTML file which is a WC content template page, (for arguments sake called cDep_2.wcx) I have the following;

<% 	
	* VS Addin Comment: SourceFile="~\..\deploy\cdeprocess.PRG"
	pcPageTitle = "cdep_2" 	
%>
<% Layout="/views/_cdep_layoutpage.wcs" %>

         <div id="xLeft" style="float:left; width:25%; background-color:cornsilk;">

             <%= HtmlDropDown("lstDept",
                             "lc_SelDept",
                             "cDept",
                             "alltrim(code)",
                             "alltrim(attribute_)",
                             "",
                             "--- Select a Department")
             %> 

		</div>
<!-- remove sections if you're not using them -->
<% section="headers" %>
<% endsection %>

<% section="scripts" %>
<% endsection %>

I believe I have populated everything correctly:

  • [1] Name of the dropdown list in the rendered code is "lstDept"
  • [2] the selected value goes to the variable lc_SelDept
  • [3] data to populate the list comes from the cursor cDept
  • [4] the attribute in the file to get the 'value' for/from the list is the variable "code"
  • [5] the text to populate the dropdown list comes from the field 'attribute_'
  • [6] no other html required
  • [7] the text first seen in the list is "--- Select a Department"

The dropdown renders perfectly, but, how do I pull the selected values specified when the user selects from the drop down list? I thought it might be in the lc_selDept variable but it appears not to change.

I've tried display it simply as;

The selected value is:
<br/>
  <%= lc_SelDept %>
<br/>

but I get a message that this renders an error.

Can someone help in letting me know how to pull the value from the selected list?

thx Serge

Gravatar is a globally recognized avatar based on your email address. re: Retrieving the selected value from an HTMLDropDown list
  Rick Strahl
  Serge
  Dec 1, 2019 @ 11:34am

First you need to make sure that the drop down is inside of a <form> tag that submits the form to the server (or an AJAX request which will be different):

<form action="">
   <%= HtmlDropDown(...)


   <button type="submit">Send</button>
</form>

Use an empty action="" to post back to the same page you came from, otherwise give it a new URL.

You also need a button to submit the form although you can add an AutoPostback="true" to the control to force it to submit. You may still need a button (invisible if necessary) to submit the form reliably in all browsers.

Assuming this is a form post to capture the value then:

lcSelVal = Request.Form("lstDept")

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Retrieving the selected value from an HTMLDropDown list
  Serge
  Rick Strahl
  Dec 2, 2019 @ 01:04pm

Hi Rick,

Thanks - following your instructions the code worked perfectly. Once I saw how the framework was returning the data back from the form to the server it was easy to trap that data and use this information in code.

thx Rick! Serge

© 1996-2019