Formatting a date in a SharePoint DataFormWebPart DropDownList
Here’s a trip down memory lane for those of you still working with SharePoint 2007 going down the SharePoint Designer application development path… I had a request to format a DVDropDownList displaying a date.There’s a lot of posts out there with clever javascript hacks, and the obvious method of using a calculated column to format the text just doesn’t work (you end up with type information being displayed as well as your data). Luckily, inheritance saves the day.
As can be seen at https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.dvdropdownlist.aspx, The DVDropDownList inherits from ListControl, so our trusty ASP.NET skills come in handy – there’s a property “DataTextFormatString” (https://msdn.microsoft.com/EN-US/library/system.web.ui.webcontrols.listcontrol.datatextformatstring) that applies a standard String.Format to the field referenced in DataTextField before rendering, so we can leverage the date formatting capabilities built into that function: https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx. Note: this method would also let you format types other than DateTime - you want, such as if you need to control currency or decimal places, or even to cheesily place arbitrary text before or after your textfield.
So, I ended up with some code in the form:
<SharePoint:DVDropDownList runat="server" id="ff2{$Pos}" selectedvalue="{@Date}" __designer:bind="{ddwrt:DataBind('i',concat('ff2',$Pos),'SelectedValue','SelectedIndexChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Date')}" datasourceid="spdatasource1" datatextfield="Date" dataTextFormatString="{{0:MM/dd/yyyy (dddd)}}" datavaluefield="Date" />
Comments
- Anonymous
March 08, 2011
Why the double curly braces? Because you're inside a XSLT parser, and { and } are reserved characters....