How to use Case statement in RenderPattern of a Custom Field Type

By Namwar Rizvi - Last updated: Wednesday, October 14, 2009 - Save & Share - 2 Comments

While designing a custom field type, you may sometime need to render different HTML based on the value of field. For example, if you want to display Red,Amber, Green traffic light images to highlight High,Medium, Low value then you need to dynamically switch image url based  on the value of the field.

In CAML, you have Case structure to implement this switching logic. It works very much like the C# switch-case statement. Following is an example of RenderPattern of a custom field which display different images based on the value of the field.

<RenderPattern Name="DisplayPattern">
      <Switch>
        <Expr>
          <Column />
        </Expr>
        <Case Value="1" >
          <HTML><![CDATA[<img alt="Red" src="/_layouts/IMAGES/ewr210m.gif" />]]></HTML>
         </Case>
        <Case Value="2" >
          <HTML><![CDATA[<img alt="Amber" src="/_layouts/IMAGES/ewr211m.gif"]]></HTML>
        </Case>
        <Case Value="3" >
          <HTML><![CDATA[<img alt="Green" src="/_layouts/IMAGES/ewr209m.gif"]]></HTML>
        </Case>
        <Default>
            <HTML><![CDATA[<span>No Value</span>]]></HTML>
        </Default>
      </Switch>
    </RenderPattern>

Share
Posted in Tips • Tags: , , , Top Of Page

2 Responses to “How to use Case statement in RenderPattern of a Custom Field Type”

Comment from gaurav kanwar
Time August 19, 2010 at 7:48 am

Hi,
Thank you for putting this post. I was exactly looking for it. Great Job

Comment from goksenin
Time March 28, 2013 at 10:03 am

Hi,

Thanks for the post.I need to use the “greater than” expression on this post.For example;
case value>7
case 7>value>3
case 3>value

I need these 3 cases.So How can I integrate these expressions to this code?

Write a comment