persisting visible attribute in tab control

M

magister

Hello,

I have a tab control, but for some reason the visible attribute
doesn't get persisted, can anyone tell me why?

Thanks, code below


using System;
using System.IO;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace HitecLabs.Conform.UI
{
/// <summary>
/// A WebControl that displays multiple tabs, like dividers in a
notebook or
/// labels in a set of folders in a filing cabinet. The tabs can
contain other controls.
/// The control supports both the Standard and the Outlook style.
/// </summary>
[
ToolboxData("<{0}:TabControl runat=server></{0}:TabControl>"),
ControlBuilder(typeof(TabControlBuilder)),
System.Drawing.ToolboxBitmap(typeof(TabControl),
"Resources.ToolboxBitmaps.TabControl.bmp"),
Designer(typeof(TabControlDesigner)),
ParseChildren(true, "Tabs"),
PersistChildren(false),
DefaultProperty("Tabs"),
DefaultEvent("SelectedIndexChanged"),
]
public class TabControl : Control, IPostBackDataHandler,
IPostBackEventHandler
{
#region Enums
/// <summary>
/// Specifies the Orientation of the TabControl.
/// </summary>
public enum TabOrientation
{
Vertical,
Horizontal
}
#endregion



#region Fields
private TabCollection _tabs = new TabCollection();
private Unit _unitWidth = new Unit("100%");
private Unit _unitHeight = new Unit("100%");
private TabOrientation _tabOrientation = TabOrientation.Vertical;
private bool _autoPostBack = true;
private int _selectedIndex = 0;
#endregion



#region Properties, Converters and Indexers
/// <summary>
/// A collection of Tabs for the TabControl
/// </summary>
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerDefaultProperty),
Description("A collection of Tabs for the TabControl"),
]
public TabCollection Tabs
{
get{ return _tabs; }
}

/// <summary>
/// Sets or retrieves the Width of the TabControl
/// </summary>
[
DefaultValue("100%"),
Description("The Width of the TabControl")
]
public Unit Width
{
get { return _unitWidth; }
set { _unitWidth = value; }
}

/// <summary>
/// Sets or retrieves the Height of the TabControl
/// </summary>
[
DefaultValue("100%"),
Description("The Height of the TabControl")
]
public Unit Height
{
get { return _unitHeight; }
set { _unitHeight = value; }
}

/// <summary>
/// Sets or retrieves the Orientation of the TabControl,
Vertical=Outlook, Horizontal=Standard
/// </summary>
[
DefaultValue(TabOrientation.Vertical),
Description("The Orientation of the TabControl, Vertical=Outlook,
Horizontal=Standard")
]
public TabOrientation Orientation
{
get { return _tabOrientation; }
set { _tabOrientation = value; }
}

/// <summary>
/// Sets or retrieves whether the TabControl state automatically
posts back to the server when a tab is clicked.
/// </summary>
[
DefaultValue(true),
Description("Indication whether the TabControl state automatically
posts back to the server when a tab is clicked.")
]
public bool AutoPostBack
{
get { return _autoPostBack; }
set { _autoPostBack = value; }
}

/// <summary>
/// Sets or retrieves the zero-based index of the currently selected
Tab in the TabControl.
/// </summary>
[
Browsable(false)
]
public int SelectedIndex
{
get { return _selectedIndex; }
set { _selectedIndex = value; }
}
#endregion



#region Overrides
protected override void OnInit(EventArgs e)
{
base.OnInit(e);

if (AutoPostBack && Page != null)
{
Page.RegisterRequiresPostBack(this);
}
}

protected override void AddParsedSubObject(Object obj)
{
// This method is not necessary when ParseChildren(true, "Tabs") is
used.

if (obj is Tab)
{
_tabs.Add((Tab)obj);
}
}

protected override void LoadViewState(Object viewState)
{
if (viewState != null && _autoPostBack)
{
_selectedIndex = Int32.Parse((String) viewState);
}
else
{
base.LoadViewState(viewState);
}
}

protected override Object SaveViewState()
{
// If AutoPostBack is set, save the SelectedTab to the view state
for postback scenarios
if (_autoPostBack)
{
return _selectedIndex.ToString();
}
else
{
return base.SaveViewState();
}
}

/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
// Register the client script.
if (Page != null &&
!Page.IsClientScriptBlockRegistered(this.GetType().FullName))
{
Page.RegisterClientScriptBlock(this.GetType().FullName, "");
}

if (_tabs.Count > 0)
{
// Get the Selected Index.
// For a non-AutoPostBack TabControl, the selected index is stored
in a cookie.
// Otherwise, it is stored as a property of the TabControl.
if (!_autoPostBack && Page != null)
{
_selectedIndex = 0;
System.Web.HttpCookie cookie =
Page.Request.Cookies[this.UniqueID+"_SelectedIndex"];
if (cookie != null)
_selectedIndex = Int32.Parse(cookie.Value);
if (_selectedIndex < 0 || _selectedIndex > _tabs.Count - 1)
_selectedIndex = 0;
}

// Call the appropriate rendering method, depending on the
TabOrientation
// (This provides easier maintenance and better readability)
if (_tabOrientation == TabOrientation.Vertical)
{
RenderTabControlVertical(output);
}
else if (_tabOrientation == TabOrientation.Horizontal)
{
RenderTabControlHorizontal(output);
}
}
}

void IPostBackDataHandler.RaisePostDataChangedEvent()
{
// Stub Implementation, Required for IPostBackDataHandler
// This control must derive from IPostBackDataHandler, even though
it doesn't use its methods.
}

bool IPostBackDataHandler.LoadPostData(string strPostDataKey,
System.Collections.Specialized.NameValueCollection postDataCollection)
{
// Stub Implementation, Required for IPostBackDataHandler
// This control must derive from IPostBackDataHandler, even though
it doesn't use its methods.
return false;
}

void IPostBackEventHandler.RaisePostBackEvent(String
strEventArgument)
{
if (strEventArgument == null)
return;

_selectedIndex = Int32.Parse(strEventArgument);

EventArgs e = new EventArgs();
OnSelectedIndexChanged(e);
}
#endregion



#region Methods
/// <summary>
/// Renders the TabControl vertically to the output parameter
specifed.
/// </summary>
/// <param name="output">The HTML Writer to write out to </param>
private void RenderTabControlVertical(HtmlTextWriter output)
{
// TabControl
HtmlTable table = new HtmlTable();
table.ID = this.UniqueID;
table.Width = this.Width.ToString();
table.Height = this.Height.ToString();
table.CellSpacing = 1;
table.CellPadding = 0;
table.Border = 1;
table.BgColor = "Menu";
table.Style.Add("cursor", "default");

// Render each tab in the TabCollection.
foreach(Tab tab in _tabs)
{
String strDisplay = "none";
if (_selectedIndex == _tabs.IndexOf(tab) )
strDisplay = "block";



// TabHeader

HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell td = new HtmlTableCell();
if (_selectedIndex == _tabs.IndexOf(tab) )
{
td.Attributes.Add("class", "TabVertical_HeaderSelected");
}
else
{
td.Attributes.Add("class", "TabVertical_Header");
}
td.Attributes.Add("orientation",
this.Orientation.ToString().ToLower());
if (_autoPostBack)
{
td.Attributes.Add("onclick", "jscript:" +
Page.GetPostBackEventReference(this, _tabs.IndexOf(tab).ToString()));
}
td.Attributes.Add("oncontextmenu", "TabVertical_OnContextMenu()");
td.Controls.Add(new LiteralControl(tab.HeaderText));
tr.Controls.Add(td);
table.Controls.Add(tr);



// TabView

// Only render the TabView under the following conditions:
// (1) AutoPostBack is set to false.
// (2) AutoPostBack is set to true, and the TabView is for the
Selected Tab
if (!_autoPostBack || (_autoPostBack && _selectedIndex ==
_tabs.IndexOf(tab)) )
{
tr = new HtmlTableRow();
td = new HtmlTableCell();
td.Attributes.Add("class", "TabVertical_Content");
td.Controls.Add(tab);
tr.Controls.Add(td);
tr.Style.Add("display", strDisplay);
table.Controls.Add(tr);
}
}

table.RenderControl(output);
}

/// <summary>
/// Renders the TabControl horizontally to the output parameter
specifed.
/// </summary>
/// <param name="output">The HTML Writer to write out to </param>
private void RenderTabControlHorizontal(HtmlTextWriter output)
{
// TabControl
HtmlTable table = new HtmlTable();
table.ID = this.UniqueID;
table.Width = this.Width.ToString();
table.Height = this.Height.ToString();
table.CellSpacing = 0;
table.CellPadding = 0;
table.Border = 0;
table.BgColor = "Menu";
table.Style.Add("cursor", "default");

HtmlTableRow tr = new HtmlTableRow();

int nWidthPercent = 15;
if (_tabs.Count * nWidthPercent > 100)
{
nWidthPercent = 100 / _tabs.Count;
}

// Render each tab in the TabCollection.
foreach(Tab tab in _tabs)
{
// TabHeader

HtmlTableCell td = new HtmlTableCell();
if (_selectedIndex == _tabs.IndexOf(tab) )
{
td.Attributes.Add("class", "TabHorizontal_HeaderSelected");
}
else
{
td.Attributes.Add("class", "TabHorizontal_Header");
}
if (_autoPostBack)
{
td.Attributes.Add("onclick", "jscript:" +
Page.GetPostBackEventReference(this, _tabs.IndexOf(tab).ToString()));
}
td.Attributes.Add("orientation",
this.Orientation.ToString().ToLower());
td.Style.Add("width", nWidthPercent.ToString() + "%");
td.Controls.Add(new LiteralControl(tab.HeaderText+"&nbsp;"));
tr.Controls.Add(td);
}
// HtmlTableCell tdSpace = new HtmlTableCell();
// tdSpace.Controls.Add(new LiteralControl("&nbsp;"));
// tdSpace.Attributes.Add("class", "TabHorizontal_Space");
// tr.Controls.Add(tdSpace);
table.Controls.Add(tr);

// TabView
// foreach (Tab tab in _tabs)
// {
// String strDisplay = "none";
// if (_selectedIndex == _tabs.IndexOf(tab) )
// strDisplay = "block";
//
// // Only render the TabView under the following conditions:
// // (1) AutoPostBack is set to false.
// // (2) AutoPostBack is set to true, and the TabView is for the
Selected Tab
// if (!_autoPostBack || (_autoPostBack && _selectedIndex ==
_tabs.IndexOf(tab)) )
// {
// tr = new HtmlTableRow();
// HtmlTableCell td = new HtmlTableCell();
// td.ColSpan = _tabs.Count + 1;
// td.Attributes.Add("class", "TabHorizontal_Content");
// if (tab.HasControls() == false)
// {
// tab.Controls.Add(new LiteralControl("&nbsp;"));
// }
// td.Controls.Add(tab);
// tr.Controls.Add(td);
// tr.Style.Add("display", strDisplay);
// table.Controls.Add(tr);
// }
// }
table.RenderControl(output);
}
#endregion



#region Delegates and Events
/// <summary>
/// Occurs when the current Tab selection is changed.
/// </summary>
[Description("Occurs when the current Tab selection is changed.")]
public event EventHandler SelectedIndexChanged;

/// <summary>
/// Raises the SelectedIndexChanged event.
/// Inheritors must also call this version.
/// </summary>
/// <param name="e">An EventArgs object that contains the event
data. </param>
public virtual void OnSelectedIndexChanged(EventArgs e)
{
if (SelectedIndexChanged != null)
SelectedIndexChanged(this, e);
}
#endregion
}


/// <summary>
/// Interacts with the parser to build a TabControl.
/// </summary>
public class TabControlBuilder : ControlBuilder
{
#region Fields
private static TagPrefixAttribute s_tagPrefix = null;
#endregion


#region Overrides
public override Type GetChildControlType(String strTagName,
IDictionary attributes)
{
// Get the predefined TagPrefix (if exists) from this Assembly
if (s_tagPrefix == null)
{
System.Reflection.Assembly assembly =
System.Reflection.Assembly.GetAssembly(typeof(TabControl));
object[] tagPrefixes =
assembly.GetCustomAttributes(typeof(TagPrefixAttribute), false);
if (tagPrefixes.Length > 0)
{
s_tagPrefix = (TagPrefixAttribute) tagPrefixes[0];
}
}

Type type = null;

// Check to see if the current tag is a Tab.
if (s_tagPrefix != null)
{
if ( String.Compare(strTagName, s_tagPrefix.NamespaceName +
":tab", true) == 0 ||
String.Compare(strTagName, s_tagPrefix.TagPrefix + ":tab", true)
== 0 )
{
type = typeof(Tab);
}
}

return type;
}

public override bool AllowWhitespaceLiterals()
{
return false;
}
#endregion
}



/// <summary>
/// A collection of Tabs to be used by the TabControl.
/// </summary>
public class TabCollection : CollectionBase
{
#region Properties, Converters and Indexers
/// <summary>
/// Gets the Tab at the specified index.
/// </summary>
public Tab this[int nIndex]
{
get { return (Tab) base.List[nIndex]; }
}
#endregion


#region Methods
/// <summary>
/// Adds a new Tab to the collection
/// </summary>
/// <param name="tab">The Tab to be added</param>
public void Add(Tab tab)
{
base.List.Add(tab);
}

/// <summary>
/// Retrieves the index of the specified Tab in the collection.
/// </summary>
/// <param name="tab">The Tab whose index is to be
retrieved.</param>
/// <returns>/// <returns>The zero-based index of the specified
Tab</returns></returns>
public int IndexOf(Tab tab)
{
return base.List.IndexOf(tab);
}
#endregion
}



/// <summary>
/// Represents a single Tab in a TabControl
/// </summary>
[
ToolboxItem(false),
DefaultProperty("LangStringID"),
]
public class Tab : Control
{
#region Fields
private String m_strHeaderText;
#endregion


#region Properties, Converters and Indexers
[
Description("The header text for the Tab"),
]
public String HeaderText
{
get { return m_strHeaderText; }
set { m_strHeaderText = value; }
}
#endregion
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top