CodeCharge Studio All ASP PHP Perl ColdFusion Java C#.NET VB.NET

Retrieve Values From a Multi-Select ListBox

This example shows how to retrieve values from a Listbox which allows for the selection of multiple values. In this case, we have a Grid called Tasks which receives an input parameter called s_Project. The value of s_Project originates from a multiselect Listbox control in a Search form.

  1. Set the Multiple property in the Format tab of the Listbox or modify the tag in HTML mode by entering multiple in the tag.
    <select name="{s_Project_Name}" multiple>
  2. Add the Before Build Select event to the grid.
  3. Within the event, add the code below:

ASP

Function Tasks_DataSource_BeforeBuildSelect(Sender) 
' "s_Project" contains the comma delimited string with the selected values

  if CCGetParam("s_Project", Empty) <>  Empty then
    If Tasks.DataSource.Where <> Empty Then _
      Tasks.DataSource.Where = Tasks.DataSource.Where & " AND "

    Tasks.DataSource.Where = Tasks.DataSource.Where &_
         "task_project_id IN ("&CCGetParam("s_Project", Empty) &")"
  End if

End Function

PHP

function Tasks_DataSource_BeforeBuildSelect(& $sender) {
global $Tasks;

  $s_Project = CCGetParam("s_Project", ""); 
  if (count($s_Project) > 0 AND is_array($s_Project)) {
    foreach ($s_Project as $key => $value) {
      if ($Projects != "") $Projects = $Projects.",";
      $Projects = $Projects.$value;
    }
    if ($Tasks->DataSource->Where != "") 
	  $Tasks->DataSource->Where .= " AND ";
    if ($Projects != "") 
	  $Tasks->DataSource->Where .= " tasks.project_id IN (".$Projects .")";
  }
}

Perl

sub Tasks_DataSource_BeforeBuildSelect() {

  $s_Project = CCGetParam("s_Project", "");

  if (ref($s_Project) eq "ARRAY") {
    for ($i=0; $i <= $#{$s_Project}; $i++  ) {
      if ($Projects ne "") {
	    $Projects .= ",";
      }
      $Projects .= $s_Project->[$i];
    }
  } elsif (length($s_Project)) {
     $Projects = $s_Project;
  }
  
  if ($Tasks->{DataSource}->{Where} ne "") {
      $Tasks->{DataSource}->{Where} .= " AND ";
  }
  if ($Projects ne "" ) {
      $Tasks->{DataSource}->{Where} .= " tasks.project_id IN (".$Projects .")";
  }
}

ColdFusion

<!---Tasks_BeforeBuildSelect --->
  <CFMODULE Template="CCGetParam.cfm" strName="s_Project" outputVar="Projects">
  <!-- "Projects" contains the comma delimited string with the selected values -->
  <CFIF Projects NEQ "">
    <CFIF strWhere NEQ ""> 
        <CFSET strWhere=strWhere & " AND ">
    </CFIF>
    <CFSET strWhere=strWhere & " tasks.project_id IN (#Projects#)">
  </CFIF>

Java

//Tasks_BeforeBuildSelect

  String[] s_Project = e.getPage().getHttpGetParams().getParameterValues("s_Project"); 

  if (s_Project != null && s_Project.length > 0 ) {
    StringBuffer projects = new StringBuffer();
    for ( int i = 0; i < s_Project.length; i++ ) {
      if ( projects.length() > 0 ) {
        projects.append(", ");		
      }
      projects.append(s_Project[i]);
    }
    StringBuffer where = null;
    if ( ! StringUtils.isEmpty(e.getCommand().getWhere()) ) {
      where = new StringBuffer(e.getCommand().getWhere()).append(" AND ");
    } else {
      where = new StringBuffer();
    }

    if (projects.length() > 0) {      
      where.append("tasks.project_id IN ("+projects.toString()+")");
      e.getCommand().setWhere(where.toString());
    }
  }
 

VB.Net

'Tasks_BeforeBuildSelect

If IsNothing(System.Web.HttpContext.Current.Request("s_Project")) Then
    CType(Select, TableCommand).Where = "tasks.project_id IN (" + System.Web.HttpContext.Current.Request("s_Project") +")"
    CType (Select, TableCommand).Operation = "AND"
    CType (Count, TableCommand).Where = "tasks.project_id IN (" + System.Web.HttpContext.Current.Request("s_Project") + ")"
    CType (Count, TableCommand).Operation = "AND"
End If

C#

//Tasks_BeforeBuildSelect

if(System.Web.HttpContext.Current.Request["s_Project"]!=null)
{
    ((TableCommand)Select).Where = "tasks.project_id IN (" + System.Web.HttpContext.Current.Request["s_Project"] +")";
    ((TableCommand)Select).Operation = "AND";
    ((TableCommand)Count).Where = "tasks.project_id IN (" + System.Web.HttpContext.Current.Request["s_Project"] + ")";
    ((TableCommand)Count).Operation = "AND";
}

See Also: