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

Changing Text Color

This example shows how to change the value of a Label control before it is shown. In particular, we have a Grid called Tasks which has a Label field called Priority. We shall add event code so that the color of the Label text changes depending on the field value (red for high, black for normal, and green for low priority).

Method 1

Use custom HTML
  1. Set the Content property of the Label field to HTML.
  2. Add the Before Show event to the Priority Label.
  3. Within the event, add the code below:

ASP

Function Tasks_Priority_BeforeShow(Sender)

   If Tasks.Priority.Value = "High" Then 
      Tasks.Priority.Value = "<font color=RED>"&Tasks.Priority.Value&"</font>"
   Elseif Tasks.Priority.Value = "Low" Then 
      Tasks.Priority.Value = "<font color=GREEN>"&Tasks.Priority.Value&"</font>"
   End if 

End Function

PHP

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

  if ($Tasks->Priority->GetValue() == "High") {
    $Tasks->Priority->SetValue("<font color=RED>".$Tasks->Priority->GetValue()."</font>"); 
  } else if ($Tasks->Priority->GetValue() == "Lowest") {
    $Tasks->Priority->SetValue("<font color=GREEN>".$Tasks->Priority->GetValue()."</font>");
  }
  
}

Perl

sub Tasks_Priority_BeforeShow() {

  if ($Tasks->{Priority}->GetValue() eq "High") {
    $Tasks->{Priority}->SetValue("<font color=RED>".$Tasks->{Priority}->GetValue()."</font>");
  } elsif ($Tasks->{Priority}->GetValue() eq "Lowest") {
    $Tasks->{Priority}->SetValue("<font color=GREEN>".$Tasks->{Priority}->GetValue()."</font>");
  }
  
}

ColdFusion

<!---Tasks_Priority_BeforeShow --->

<CFIF fldPriority EQ "High">
  <CFSET fldPriority="<font color=RED>#fldPriority#</font>">
<CFELSEIF fldPriority EQ "Lowest">
  <CFSET fldPriority="<font color=GREENRED>#fldPriority#</font>">
</CFIF>

VB.Net

'Tasks_Priority_BeforeShow

  If TasksPriority.Text = "High" Then
    TasksPriority.Text = ("<font color=RED>"&(TasksPriority.Text).ToString()&"</font>").ToString()
  ElseIf TasksPriority.Text = "Lowest" Then
    TasksPriority.Text = ("<font color=GREEN>"&(TasksPriority.Text).ToString()&"</font>").ToString()
  End if 

C#

//Tasks_Priority_BeforeShow

  if (TasksPriority.Text == "High") {
    TasksPriority.Text = "<font color=RED>"+TasksPriority.Text+"</font>";
  } else if (TasksPriority.Text == "Lowest") {
    TasksPriority.Text = "<font color=GREEN>"+TasksPriority.Text+"</font>";
  } 

Java

//Tasks_Priority_BeforeShow

  if ("High".equals(e.getControl().getValue())) { 
      e.getControl().setValue("<font color=RED>"+e.getControl().getValue()+"</font>");
  } else if ("Low".equals(e.getControl().getValue())) { 
      e.getControl().setValue("<font color=GREEN>"+e.getControl().getValue()+"</font>");
  } 

Method 2

Use A Label control
  1. Switch to the HTML mode
  2. Add a Label control called Priority_Color into the <font> tag.

    <!-- BEGIN Row -->
      <tr>
        <td class="ClearDataTD">{task_name} </td>
        <td class="ClearDataTD">{project_name} </td>
        <td class="ClearDataTD">{status_name} </td>
        <td class="ClearDataTD"><font color="{Priority_Color}">{Priority}</font> </td>
        <td class="ClearDataTD">{date_resolve} </td>
      </tr>
    <!-- END Row -->
  3. Add the Before Show Row event to the Tasks Grid.
  4. Within the event, add the code below:

ASP

Function Tasks_BeforeShowRow()

   If Tasks.Priority.Value = "High" Then
      Tasks.Priority_Color.Value = "RED"
   ElseIf Tasks.Priority.Value = "Lowest" Then
      Tasks.Priority_Color.Value = "GREEN"
   Else
      Tasks.Priority_Color.Value = "BLACK"
   End if

End Function

PHP

function Tasks_BeforeShowRow() {
global  $Tasks;

  if ($Tasks->Priority->GetValue() == "High") {
    $Tasks->Priority_Color->SetValue("RED"); 
  } else if ($Tasks->Priority->GetValue() == "Lowest") {
    $Tasks->Priority_Color->SetValue("GREEN");
  } else {
    $Tasks->Priority_Color->SetValue("BLACK"); 
  }

}

Perl

sub Tasks_BeforeShowRow() {

  if ($Tasks1->{Priority}->GetValue() eq "High") {
    $Tasks1->{Priority_Color}->SetValue("RED");
  } elsif ($Tasks1->{Priority}->GetValue() eq "Lowest") {
    $Tasks1->{Priority_Color}->SetValue("GREEN");
  } else {
    $Tasks1->{Priority_Color}->SetValue("BLACK");
  }

}

Java

//Tasks BeforeShowRow

  if ("High".equals(e.getGrid().getControl("Priority").getValue())) { 
      e.getGrid().getControl("Priority_Color").setValue("RED");
  } else if ("Lowest".equals(e.getGrid().getControl("Priority").getValue())) { 
      e.getGrid().getControl("Priority_Color").setValue("GREEN");
  } else {
      e.getGrid().getControl("Priority_Color").setValue("BLACK");  
  }
 

See also:

Before Show event, Before Show Row event, Content property


On-line, printable versions and updates