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());
}

}

1 comment:

Anonymous said...

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

Naser_Ezzati@yahoo.com