When using the thwcTabs and thwcTree libraries with the UseCookies property of
the MainMenu set to true user choices can be saved in cookies and later
restored when the page is refreshed. When using custom rendering user choices
can still be persisted to cookies but it will take custom code to recreate
their choices since the respective library will not be doing the rendering.
-
In this sample the page headers are items of a menu controlled by the thwcTree
library, they may be clicked to hide and show parts of the page in the same way
as if the thwcTree library had rendered them.
-
Keep in mind that HTML elements meant to be manipulated on the server need to have
the runat="server" attribute.
-
Note the IDs of the elements defined in this ASP.NET form and in the XML. The page
headers and the areas they contain have matching IDs in the XML file, as well as having
the runat="server" attribute.
-
The HideSiblings button at top demonstrates the effect of the hideSiblings item attribute.
By enabling it clicking on a page header will cause all the others to collapse, helping
to focus on that particular section.
By referring to the thwcTree schema information about
the cookie it saves may be found.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class scookies : System.Web.UI.Page {
private bool HideSiblings {
get { return this.ViewState["HideSiblings"] != null ? (bool) this.ViewState["HideSiblings"] : false; }
set { this.ViewState["HideSiblings"] = value; }
}
private void btnToggle_Click(object sender, EventArgs e) {
XmlNode node = this.mm.MainMenuDoc.SelectSingleNode("/mn:mainMenu/mn:itemDefaults", this.mm.NamespaceManager);
XmlAttribute hideSiblings = node.Attributes["tr:hideSiblings"];
if(hideSiblings == null) {
hideSiblings = this.mm.MainMenuDoc.CreateAttribute("tr:hideSiblings", "TimothyHumphrey.WebControls.MainMenu.Tree");
node.Attributes.Append(hideSiblings);
}
this.HideSiblings = !this.HideSiblings;
hideSiblings.Value = this.HideSiblings.ToString().ToLower();
}
private void Page_Init() {
this.btnToggle.Click += new EventHandler(btnToggle_Click);
}
private void Page_Load() {
const string cookieName = "thwcTreeMainContent_MainContent_container";
const string menuSuffix = "Container";
try {
if(this.mm.Cookies != null && this.mm.Cookies[cookieName] != null) {
//Recreate visibility status selected by user
Match m = Regex.Match(this.mm.Cookies[cookieName].InnerText, @";(.*)");
if(m.Groups[1].Success) {
string[] items = m.Groups[1].Value.Split(',');
Regex itemPattern = new Regex(@"^(.*)(\d)$");
HtmlControl control;
bool visible;
for(int i = 0; i < items.Length; i++) {
m = itemPattern.Match(items[i]);
if(m.Success) {
control = this.FindControl(m.Groups[1].Value) as HtmlControl;
visible = m.Groups[2].Value == "1";
if(control != null) {
control = this.FindControl(control.UniqueID + menuSuffix) as HtmlControl;
control.Style["display"] = visible ? String.Empty : "none";
}
}
}
}
}
}
catch {}
}
private void Page_PreRender() {
this.btnToggle.Text = ((this.HideSiblings) ? "Disable" : "Enable") + " HideSiblings";
}
}
|