views:

557

answers:

2

I'm developing a SharePoint publishing site and setting up its content types and page layouts. I need to display the value for a Year field with type Number. The markup currently is:

<SharePointWebControls:NumberField FieldName="Year" runat="server" id="Year" />

The problem with the default behaviour is that it shows each number with a comma, e.g. "2,009" instead of "2009". Is there a way I can set some sort of String.Format syntax on the field to make it display correctly?

I tried creating a new rendering template which looks like this:

<SharePoint:RenderingTemplate ID="YearNumberField" runat="server">
  <Template>
    <SharePoint:FormField ID="TextField" runat="server"/>
  </Template>
</SharePoint:RenderingTemplate>

... but there doesn't appear to be any 'Format' property on the FormField object.

Thanks for any help.

Update:

I tried wrapping the SharePoint:FormField tag inside SharePoint:FormattedString. Unfortunately the field was not formatted, same results as this question.

A: 

from Just Another SharePoint Blog

Open the list view in SharePoint Designer.

Right click on the data view web part. (the list)

Select Convert to XSLT Data View

Click on the number field you would like to format

A > will appear showing Data Field, Format As

Click on the link below Format As - Number formatting options

Under Options deselect Use 1000 separator

Click OK

Save your changes and hit F12 to preview

Steve Ruiz
Thanks Steve but this is in a page layout, there are no web parts involved.
Alex Angas
A: 

The issue is that the rendering template must use FormField. This always renders the value in the format: 1,989&nbsp;. To resolve this the rendered text needs to be trapped and altered to get the desired output. Here are two approaches to resolving this:

1. Write a custom control inherited from NumberField

The RenderFieldForDisplay and RenderFieldForInput methods can be overridden to provide the desired output. Additional properties can be added to the control to describe additional behaviour.

Pros: No changes to rendering templates required.

2. Write a custom control for use in the rendering template

A control that (for example) uses regular expressions to alter text can wrap around the FormField control.

<SharePoint:RenderingTemplate ID="YearField" runat="server">
    <Template>
     <RX:RegexManipulatorControl runat="server"
       Mode="Replace"
       Expression=","
       Replacement="">
      <SharePoint:FormField runat="server"/>
     </RX:RegexManipulatorControl>
    </Template>
</SharePoint:RenderingTemplate>

Pros: Generic solution can be used for any type of field.

Alex Angas