// FieldSet.cs
// Copyright (C) 2013 Pedro Fernandes
// This program is free software; you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU General Public License for more details. You should have received a copy of the GNU
// General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59
// Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace NewLife.Bootstrap.Controls
{
[ToolboxData("<{0}:FieldSet runat=server></{0}:FieldSet>")]
[ParseChildren(true, "Content")]
public class FieldSet : WebControl, INamingContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="FieldSet" /> class.
/// </summary>
public FieldSet()
{
this.Legend = "";
}
/// <summary>
/// Gets or sets the content.
/// </summary>
/// <value>
/// The content.
/// </value>
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateContainer(typeof(FieldSet))]
[TemplateInstance(TemplateInstance.Single)]
public virtual ITemplate Content
{
get;
set;
}
/// <summary>
/// Gets or sets the legend.
/// </summary>
/// <value>
/// The legend.
/// </value>
[Category("Appearance")]
[DefaultValue("")]
public string Legend
{
get { return (string)ViewState["Legend"]; }
set { ViewState["Legend"] = value; }
}
/// <summary>
/// Renders the HTML opening tag of the control to the specified writer. This method is used primarily by control developers.
/// </summary>
/// <param name="writer">A <see cref="T:System.Web.UI.HtmlTextWriter" /> that represents the output stream to render HTML content on the client.</param>
public override void RenderBeginTag(HtmlTextWriter writer)
{
writer.RenderBeginTag(HtmlTextWriterTag.Fieldset);
}
/// <summary>
/// Renders the HTML closing tag of the control into the specified writer. This method is used primarily by control developers.
/// </summary>
/// <param name="writer">A <see cref="T:System.Web.UI.HtmlTextWriter" /> that represents the output stream to render HTML content on the client.</param>
public override void RenderEndTag(HtmlTextWriter writer)
{
writer.RenderEndTag();
}
/// <summary>
/// Renders the control to the specified HTML writer.
/// </summary>
/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter" /> object that receives the control content.</param>
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
if (!String.IsNullOrEmpty(this.CssClass)) writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
if (!String.IsNullOrEmpty(this.Width.ToString())) writer.AddStyleAttribute(HtmlTextWriterStyle.Width, this.Width.ToString());
if (!String.IsNullOrEmpty(this.Height.ToString())) writer.AddStyleAttribute(HtmlTextWriterStyle.Height, this.Height.ToString());
base.Render(writer);
}
/// <summary>
/// Renders the contents.
/// </summary>
/// <param name="output">The output.</param>
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(String.Format("<legend>{0}</legend>", this.Legend));
this.RenderChildren(output);
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);
// Initialize all child controls.
this.CreateChildControls();
this.ChildControlsCreated = true;
}
/// <summary>
/// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
/// </summary>
protected override void CreateChildControls()
{
var container = new Control();
this.Content.InstantiateIn(container);
this.Controls.Clear();
this.Controls.Add(container);
}
}
}
|