Wednesday, May 20, 2009

Different behavior in New and Edit form for a custom field

A request from a client was to create a custom field for a SharePoint list with different behavior in New form than in Edit. I was able to control this using JavaScript and the only thing left to do is to know in which form the field is rendered.

For this I had to override the RenderFieldForInput method provided by BaseFieldControl class and check the SPControlMode.

public class MyCustomField : BaseFieldControl 
{
....

protected override void RenderFieldForInput(HtmlTextWriter output)
{
string pathToScript = String.Empty;
if (this.Field != null) pathToScript = (this.ControlMode == SPControlMode.Edit) ? "scriptforedit.js" : "scriptforother.js";
output.Write("<script type=\"text/javascript\" src=\""+ pathToScript +"\" ></script>");
base.RenderFieldForInput(output);
}

No comments:

Post a Comment