Form Controls are new to Community Server 2007 as part of Chameleon and bring new capabilities to the business-as-usual process of creating ASP.NET forms. One of the features of Chameleon Form Controls I like is the ease in setting the visibility of individual controls.
The Chameleon Form I created today demonstrates how to easily display success messages on submit using the <SuccessActions /> and SetVisibilityAction control. Observing the bold code below we see that the "SuccessMsg" CSResourceControl displays on form success and the "FormArea" CSPlaceholder control is hidden. Each property can act on multiple controls.
<DBVTSiteControl:AdvertiseForm
AdvertisePostsDropDownListId="advertisePostsDropDownList"
AdvertiseYearDropDownListId="advertiseYearDropDownList"
DirectoryDropDownListId="directoryDropDownList"
SubmitButtonId="SubmitButton" runat="server" CssClass="BigForm">
<SuccessActions>
<CSControl:SetVisibilityAction runat="server" ControlIdsToShow="SuccessMsg"
ControlIdsToHide="FormArea" ID="VisibilityControl" />
</SuccessActions>
<FormTemplate>
<CSControl:PlaceHolder runat="server" ID="FormArea">
.....
</CSControl:PlaceHolder>
<CSControl:ResourceControl ID="SuccessMsg" runat="server"
ResourceName="AdvertiseForm_SuccessMessage"
CssClass="BigMessage" Visible="false" />
</FormTemplate>
</DBVTSiteControl:AdvertiseForm>
This is also extensible as demonstrated in, for example, the blog contact form which hides the Username and Email Address for registered users. A custom property can be created, as in this case, a ControlIdsToHideFromRegisteredUsers property. This will add the controls to hide in the Form Control's AttachChildControl() method and sets visibility to false in the Databind() method.
protected override void AttachChildControls()
{
.....
string[] controlIds = ControlIdsToHideFromRegisteredUsers.Split(',');
ControlsToHideFromRegisteredUsers = new List<Control>();
if (controlIds != null && controlIds.Length > 0)
{
foreach (string controlId in controlIds)
{
Control c = WeblogControlUtility.Instance().FindControl(this, controlId);
if (c != null)
ControlsToHideFromRegisteredUsers.Add(c);
}
}
}
public override void DataBind()
{
.....
if (Page.Request.IsAuthenticated)
{
foreach (Control c in this.ControlsToHideFromRegisteredUsers)
{
c.Visible = false;
}
}
}