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

Dynamically Set the Default Value of a ListBox

This example shows how to dynamically set a default value for a control. In this case, we have a Listbox called MonthBox which is located in a Record form called Registration. The event code checks for an input parameter called MonthBox and if the input parameter is not available, the Listbox value is set to the current month.

  1. Add the Before Show event for the list box.
  2. Within the event, add the code below:

ASP

Function Registration_MonthBox_BeforeShow(Sender)

  If IsEmpty(CCGetParam("MonthBox",Empty)) Then
      Registration.MonthBox.Value = Month(NOW())
  End if

End Function

PHP

function Registration_MonthBox_BeforeShow(& $sender) {
global $Registration;

  if (CCGetParam("MonthBox","") == "") {
      $Registration->MonthBox->SetValue(date("n"));
  }

}

Perl

sub Registration_MonthBox_BeforeShow() {

  $mon = (localtime())[4];
  if (CCGetParam("MonthBox","") eq "") {
    $Registration->{MonthBox}->{Value} = $mon+1;
  }

}

ColdFusion

<!---Registration_MonthBox_BeforeShow --->
  <CFMODULE Template="CCGetParam.cfm" strName="MonthBox">
  <CFIF CCGetParam EQ "">
     <CFSET fldMonthBox=DateFormat(Now(),"m")>
  </CFIF>

VB.Net

'Registration_MonthBox_BeforeShow

  If IsNothing(Request.QueryString("MonthBox")) Or Request.QueryString("MonthBox") = "" Then
      RegistrationMonthBox.Value = Month(NOW())
  End if

C#

//Registration_MonthBox_BeforeShow
  if (Request.QueryString["MonthBox"] == null || Request.QueryString["MonthBox"].Length == 0) {
      RegistrationMonthBox.Value = DateTime.Now.Month.ToString();
  }

Java

//Registration_MonthBox_BeforeShow

  if (StringUtils.isEmpty(e.getPage().getParameter("MonthBox"))) {
    e.getControl().setValue(new java.text.SimpleDateFormat("M").format(new Date()));
  }

See Also:

Before Show event


On-line, printable versions and updates