Description
The SPControlMode enumeration enables you to test or set the mode for controls on a form. The SPControlMode enumeration contains four possible values, Display, Edit, New, and Invalid. If the FormMode of a form is SPControlMode.Display, controls on the form will be read only. If the FormMode of a form is SPControlMode.Edit, controls on the form will be editable. If the FormMode of a form is SPControlMode.New, the form will be blank to enable a user to add a new item. Finally the FormMode of a form could be SPControlMode.Invalid. ControlMode.Invalid is the default value for a form FormMode, and indicates that the FormMode has not been set.
Usage Scenarios
You would typically use the SPControlMode enumeration to perform conditional logic in your code, for example to change the way that a control is rendered on a Web Part based on the state of the form. You would also use the SPControlMode enumeration to change the state of a form; for example, as part of a button event handler.
The following code samples show how to test the current mode of the form and display a message accordingly in the Render method of a Web Part. The samples also show how to change the current mode of a form in a custom SetFormMode method.
C# Code Sample
protected override void Render(HtmlTextWriter writer)
{
if (SPContext.Current.FormContext.FormMode == SPControlMode.Display)
{
writer.Write("SPControlMode: Display");
}
if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
{
writer.Write("SPControlMode: Edit");
}
if (SPContext.Current.FormContext.FormMode == SPControlMode.New)
{
writer.Write("SPControlMode: New");
}
if (SPContext.Current.FormContext.FormMode == SPControlMode.Invalid)
{
writer.Write("SPControlMode: Invalid");
}
}
private void SetFormMode(SPControlMode mode)
{
SPContext.Current.FormContext.SetFormMode(mode, true);
}
Visual Basic .NET Code Sample
Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
If SPContext.Current.FormContext.FormMode = SPControlMode.Display Then
writer.Write("SPControlMode: Display")
End If
If SPContext.Current.FormContext.FormMode = SPControlMode.Edit Then
writer.Write("SPControlMode: Edit")
End If
If SPContext.Current.FormContext.FormMode = SPControlMode.[New] Then
writer.Write("SPControlMode: New")
End If
If SPContext.Current.FormContext.FormMode = SPControlMode.Invalid Then
writer.Write("SPControlMode: Invalid")
End If
End Sub
Private Sub SetFormMode(ByVal mode As SPControlMode)
SPContext.Current.FormContext.SetFormMode(mode, True)
End Sub