I have created a custom form where I add "Mode
" parameter to query string based on the type of form I want to display: New/Display/Edit. Based on the mode from query string I set the ControlMode
property of FormFields from code-behind.
I have attached custom forms in a Web-scoped Feature:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
web.AllowUnsafeUpdates = true;
SPList myList = web.Lists.TryGetList("MyList");
if (myList != null)
{
myList.ContentTypes["MyCustomContentType"].EditFormUrl = "_layouts/MyProject/MyForm.aspx?Mode=Edit";
myList.ContentTypes["MyCustomContentType"].NewFormUrl = "_layouts/MyProject/MyForm.aspx?Mode=New";
myList.ContentTypes["MyCustomContentType"].DisplayFormUrl = "_layouts/MyProject/MyForm.aspx?Mode=Display";
myList.ContentTypes["MyCustomContentType"].Update();
}
web.AllowUnsafeUpdates = false;
}
Nonetheless there is a need to specify a ControlMode for FormField controls in aspx page, otherwise an error occurs when displaying a page. And that's where the problem begins:
if I set them to
ControlMode="New"
in aspx and setting toControlMode.New
from code-behind - New Form is okif I set them to
ControlMode="New"
in aspx and setting toControlMode.Display
from code-behind - SPContext.Current.ItemId is 0 so the form is not created as Display Formif I set them to
ControlMode="Display"
in aspx and setting toControlMode.New
from code-behind - a folder is created instead of itemif I set them to
ControlMode="Display"
in aspx and setting toControlMode.Display
from code-behind - Display Form is ok
I've tried setting the ControlMode
property from different event like OnPreInit
, OnInit
, CreateChildControls
, Page_Load
without success.
I've even tried to set the properties in aspx literally from code-behind using tricks like ControlMode="<%# _formMode %>"
also without success.
The question is: why SharePoint sticks hardly to ControlMode set in aspx page and gets confused when switching it from code-behind? Is creating separate aspx forms for each mode the only solution?