Skip to main content

Change the field display color based on its value

This is tested on WSS 3.0.

I have a custom list definition. There is a field called "Status" in this list definition. I want to change the display color of this field based on its value. Here is the steps:

In the schema.xml file of this list, find the "Status" field definition. It should be inside /List/MetaData/Fields. Based on this field, create another Computed type field. Here is the code snippet:

<Field Type="Text" DisplayName="Status" ReadOnly="TRUE" Required="TRUE" MaxLength="255" ID="{d9daabac-0014-4c1e-8c06-093ff0c7ecfc}" SourceID="{142fdfdd-7e46-4ca3-b9d3-56ae0ead646e}" StaticName="Status" Name="Status" ColName="nvarchar3" RowOrdinal="0" />
<Field ID="{0864C5F6-688C-4ee5-A4FE-3FB17CD5EE1E}" ReadOnly="TRUE" Type="Computed" Name="ColorStatus" DisplayName="Status" Dir="" DisplayNameSrcField="Status" EnableLookup="TRUE" StaticName="ColorStatus">
<FieldRefs>
<FieldRef Name="Status" />
</FieldRefs>
<DisplayPattern>
<IfEqual>
<Expr1>
<Field Name="Status" />
</Expr1>
<Expr2>
<![CDATA[OK]]>
</Expr2>
<Then>
<HTML><![CDATA[<span >]]></HTML>
<Column HTMLEncode="TRUE" Name="Status" />
<HTML><![CDATA[</span>]]></HTML>
</Then>
<Else>
<HTML><![CDATA[<span >]]></HTML>
<Column HTMLEncode="TRUE" Name="Status" />
<HTML><![CDATA[</span>]]></HTML>
</Else>
</IfEqual>
</DisplayPattern>
</Field>

The first field is the original Status field, the second field is the Computed field. If the value of the Status field is "OK", it render it as green color, otherwise render it as red.

Now you get the fields definition done, the next step you need to do is the reference this field in your view. For example, in the All Items view, reference the newly defined ColorStatus as following:

<View Type="HTML" DisplayName="All Items" Url="AllItems.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/images/generic.png" WebPartZoneID="Main">
<ViewFields>
<FieldRef Name="LinkTitle" />
<FieldRef Name="SupplierCode" />
<FieldRef Name="StartDate" />
<FieldRef Name="EndDate" />
<FieldRef Name="PreferenceCriterion" />
<FieldRef Name="Producer" />
<FieldRef Name="CountryOfOrigin" />
<FieldRef Name="NetCost" />
<FieldRef Name="ColorStatus" />
</ViewFields>
......
</View>


Now, reset IIS and refresh the All Items view, the color will change.

Comments

Popular posts from this blog

Manage IIS 7 remotely using PowerShell and AppCmd

We can use  Windows PowerShell remoting features  to manage IIS 7 websites remotely.  Currently, remoting is supported on Windows Vista with Service Pack 1 or later, Windows 7, Windows Server 2008, and Windows Server 2008 Release 2.  Start Windows PowerShell as an administrator by right-clicking the Windows PowerShell shortcut and selecting Run As Administrator .  Enable PowerShell Remoting with Enable-PSRemoting -Force Starting a Remote Session using:  Enter-PSSession -ComputerName <COMPUTER> -Credential <USER> Now the PowerShell connected to the remote server. Any commands issued with work against the remote server. We can use the Appcmd.exe command line tool to manage remote server just as what we do locally. For example, to add an application pool: c:\windows\system32\inetsrv\appcmd add apppool /name:"Contoso" /managedPipelineMode:Integrated /managedRuntimeVersion:"v4.0" /enable32BitAppOnWin64:true To change application pool for a

X509Certificate2: The system cannot find the file specified.

When I use the new X509Certificate2(fileName, password, X509KeyStorageFlags.DefaultKeySet) to create certificate from certificate file containing private key in my web application, I got following error message: System . Security . Cryptography . CryptographicException : The system cannot find the file specified . at System . Security . Cryptography . CryptographicException . ThrowCryptogaphicException ( Int32 hr ) at System . Security . Cryptography . X509Certificates . X509Utils . _LoadCertFromBlob ( Byte [] rawData , IntPtr password , UInt32 dwFlags , Boolean persistKeySet , SafeCertContextHandle & pCertCtx ) at System . Security . Cryptography . X509Certificates . X509Certificate . LoadCertificateFromBlob ( Byte [] rawData , Object password , X509KeyStorageFlags keyStorageFlags ) at System . Security . Cryptography . X509Certificates . X509Certificate2 .. ctor ( Byte [] rawData , String password , X509KeyStorageFlags keyStorageFlags ) In orde

Entity framework code first error: OriginalValues cannot be used for entities in the Added state

When I was using Entity framework code first, I encountered an error when I tried to create an entity into database. The entity is: [ Table (" EmployeeProfile ")]     public partial class EmployeeProfile     {         [ Key ]         [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]         public int EmployeeProfileID { get; set; }         [ ForeignKey ("Employee")]         public int EmployeeID { get; set; }         public virtual Employee Employee { get; set; }         [ ForeignKey (" Profile ")]         public int ProfileID { get; set; }         public virtual Profile Profile { get; set; }         [ Required ]         [ StringLength (255)]         public string ProfileValue { get; set; }     } When creating the entity, some entities have the ProfileValue="", this causes the EntityValidationException with the detailed message " OriginalValues cannot be used for entities in the Added state ". I want to allow the Prof