using System;
using System.Collections;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ChadSoft.Web.Controls
{
///
/// DataBound Tab control
///
///
/// This control will render out the list of tabs and associated content panes
/// as well as the requisite JavaScript to switch between them...
/// but it leaves the styling to the developer (via CSS).
///
///
/// <script runat="server">
/// Dictionary<string, string> content = new Dictionary<string, string>();
/// content.Add("Tab #1", "Some really cool content<br/></p>");
/// content.Add("Tab #2", "<p>Some more really cool content<br/></p>");
/// content.Add("Tab #3", "<b>Lorem Ipsum, Dude!</b><p>Blah Blah Blah!<br/></p>");
/// </script>
/// <igWeb:DhtmlTab ID="MyTab" runat="server" DataSource="<%=content%>" TabNameField="Key" />
///
public class DhtmlTab : CompositeDataBoundControl
{
private const string SCRIPT = @"function showTabPanel(tabId, frameId) { hideAllPanes(tabId); document.getElementById(tabId+'_'+frameId).style.display = ''; } function hideAllPanes(tabId) { var panes = document.getElementById(tabId+'_ContentContainer').childNodes; for(var x=0; x < panes.length; x++) { if(panes[x].className=='Panel') { panes[x].style.display = 'none'; } } }";
protected override HtmlTextWriterTag TagKey
{
get { return HtmlTextWriterTag.Div; }
}
///
/// Gets or sets the name of the field to bind to as the Tab Name.
///
/// The Tab Name field name.
[Bindable(true)]
[DefaultValue("Name")]
[Description("The object field to use as the Tab Name.")]
public string TabNameField
{
get { return ViewState["TabNameField"] as string ?? GetDefaultPropertyValue("TabNameField"); }
set { ViewState["TabNameField"] = value; }
}
///
/// Gets or sets the name of the field that contains the tab content.
///
/// The Content field name.
[Bindable(true)]
[DefaultValue("Value")]
[Description("The object field to use as the Tab Name.")]
public string ContentField
{
get { return ViewState["ContentField"] as string ?? GetDefaultPropertyValue("ContentField"); }
set { ViewState["ContentField"] = value; }
}
[Bindable(true)]
[DefaultValue("TabsContainer")]
public string TabsContainerCssClass { get; set; }
[Bindable(true)]
[DefaultValue("Tab")]
public string TabCssClass { get; set; }
[Bindable(true)]
[DefaultValue("TabContentContainer")]
public string TabContentContainerCssClass { get; set; }
[Bindable(true)]
[DefaultValue("TabContent")]
public string TabContentCssClass { get; set; }
public DhtmlTab()
{
TabsContainerCssClass = GetDefaultPropertyValue("TabsContainerCssClass");
TabCssClass = GetDefaultPropertyValue("TabCssClass");
TabContentContainerCssClass = GetDefaultPropertyValue("TabContentContainerCssClass");
TabContentCssClass = GetDefaultPropertyValue("TabContentCssClass");
}
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
{
var tabsContainer = new System.Web.UI.WebControls.Panel();
var contentContainer = new System.Web.UI.WebControls.Panel();
tabsContainer.CssClass = TabsContainerCssClass;
contentContainer.CssClass = TabContentContainerCssClass;
contentContainer.ID = "ContentContainer";
var tabCount = 0;
System.Web.UI.WebControls.Panel newTab, newPanel;
foreach (var data in dataSource)
{
var tabId = String.Format("Tab{0}", ++tabCount);
newPanel = new System.Web.UI.WebControls.Panel {
ID = tabId,
CssClass = TabContentCssClass
};
if(tabCount > 1) // Don't hide the first one
newPanel.Style["display"] = "none";
newPanel.Controls.Add(new LiteralControl(DataBinder.GetPropertyValue(data, ContentField) as String));
contentContainer.Controls.Add(newPanel);
newTab = new System.Web.UI.WebControls.Panel { CssClass = "Tab" };
newTab.Attributes["onmouseover"] = String.Format("showTabPanel('{0}', '{1}');", ClientID, tabId);
newTab.Controls.Add(new LiteralControl(DataBinder.GetPropertyValue(data, TabNameField) as String));
tabsContainer.Controls.Add(newTab);
}
Controls.Add(tabsContainer);
Controls.Add(contentContainer);
Page.ClientScript.RegisterClientScriptBlock(GetType(), "showTabPanel", SCRIPT, true);
return tabCount;
}
protected T GetDefaultPropertyValue(string propertyName)
{
var attributes = TypeDescriptor.GetProperties(this)[propertyName].Attributes;
var defaultValueAttribute =
(DefaultValueAttribute) attributes[typeof(DefaultValueAttribute)];
return (T)(defaultValueAttribute == null ? null : defaultValueAttribute.Value);
}
}
}