2007-04-19

Customizing and/or extending or customizing existing standard field controls

There are errors in the DateTimeField control in SharePoint. It crashes when you set either the DateOnly or the TimeOnly fields. We wantet just the date so I had to make a small adjustment and make MyDateTimeField control with two extra properties MyDateOnly and MyTimeOnly, and override the RenderFieldForDisplay() method. I just rendered the DateTime value myself! 3-4 lines of code only!

This article gave me the ideas neccesary to "tweak" and fix errors in the standard field controls provided by Microsoft: Stefan Gossner

Here is my code:

public class DatoFormatFieldControl : Microsoft.SharePoint.WebControls.DateTimeField
{
public bool DateOnly
{

get
{
if (ViewState["DateOnly"] == null){return false;}
return (bool)ViewState["DateOnly"];
}
set
{ ViewState["DateOnly"] = value; }
}

protected override void RenderFieldForDisplay(
HtmlTextWriter output)
{
if (DateOnly)
output.Write(((DateTime)
base.ItemFieldValue).ToShortDateString());
else
output.Write(((DateTime)
base.ItemFieldValue).ToShortDateString
() + " " + ((DateTime)
base.ItemFieldValue).ToShortTimeString());
}

}

2 comments:

Anonymous said...

Hello,
Can you send a code of an application you have just build.
Thank you.

Naser_Ezzati@yahoo.com

Anonymous said...

i'm trying to make a datetime custom field but when i edit the value it gives an error.
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

i need two fields start time and end time so i want to use a custom control to show only hours and minutes and not the date. but all i would like to achieve now is to insert a datetime to the field. can you send me an example of how you do it?

thanks edgar.cardeira [at] gmail [dotcom]