/ ? > / - / WebControl > / AntiSpamLink > / Frame > / MainMenu >

MainMenu Sample: Custom Rendering & Cookies

Preamble

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.

C# Code

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";
    }
}

XML

<?xml version="1.0" encoding="utf-8" ?>
<mainMenu
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="TimothyHumphrey.WebControls.MainMenu thwc://MainMenu/Schemas/Tree.xsd"
	xmlns="TimothyHumphrey.WebControls.MainMenu"
	xmlns:tr="TimothyHumphrey.WebControls.MainMenu.Tree"
	>
	
	<transform src="thwc://MainMenu/thwcStandard.xslt" />
	<transform src="thwc://MainMenu/thwcTree.xslt" />
	
	<menuDefaults
		uplevelTemplate=""
		downlevelTemplate=""
		permanent="true"
		hotChangedInitiators="thwcHotCI_Hover"
		visibleChangedInitiators="thwcVisibleCI_TreeClick"
		visibleChangedHandlers="thwcVisibleCH_Tree"
		/>
	<itemDefaults
		action="Special"
		keepHighlight="false"
		highlightChangedInitiators="thwcHighlightCI_TreeHover"
		highlightChangedHandlers="thwcHighlightCH_Tree"
		hotChangedInitiators="thwcHotCI_Hover"
		hotChangedHandlers="thwcHotCH_Display"
		selectedInitiators="thwcSelectedI_TreeClick"
		selectedHandlers="thwcSelectedH_Tree"
		tr:hotUnderline="true"
		tr:activeBolded="false"
		/>
	
	<menu id="MainContent_MainContent_container">
		<item id="MainContent_MainContent_preamble">
			<menu id="MainContent_MainContent_preambleContainer" />
		</item>
		<item id="MainContent_MainContent_csharp">
			<menu id="MainContent_MainContent_csharpContainer" />
		</item>
		<item id="MainContent_MainContent_xml">
			<menu id="MainContent_MainContent_xmlContainer" />
		</item>
		<item id="MainContent_MainContent_webform">
			<menu id="MainContent_MainContent_webformContainer" />
		</item>
	</menu>
</mainMenu>

ASP.NET WebForm

<%@ Page AutoEventWireup="true" Language="C#" MasterPageFile="~/thwc/thwc.master"
    Title="Custom Rendering &amp; Cookies" CodeFile="scookies.aspx.cs" Inherits="scookies" %>
<%@ Register TagPrefix="uc" TagName="Source" Src="source.ascx" %>

<asp:Content runat="server" ContentPlaceHolderID="HeadContent">
    <style type="text/css">
        .header {cursor:default;}
        pre.listing {font-size:0.7em;}
    </style>
</asp:Content>
<asp:Content runat="server" ContentPlaceHolderID="MainContent">
    <h2>MainMenu Sample: Custom Rendering &amp; Cookies</h2>
    <thwc:mainmenu runat="server" id="mm" src="scookies.xml" usecookies="true" />
    <p><asp:button runat="server" id="btnToggle" /></p>
    <div runat="server" id="container">
        <h3><span runat="server" id="preamble" class="header">Preamble</span></h3>
        <div runat="server" id="preambleContainer">
            <p>
                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.
            </p>
            <ul>
                <li>
                    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.
                </li>
                <li>
                    Keep in mind that HTML elements meant to be manipulated on the server need to have
                    the runat="server" attribute.
                </li>
                <li>
                    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.
                </li>
                <li>
                    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.
                </li>
            </ul>
        </div>
        <h3><span runat="server" id="csharp" class="header">C# Code</span></h3>
        <div runat="server" id="csharpContainer">
            <p>
                By referring to the <a href="rltree.aspx">thwcTree schema</a> information about 
                the cookie it saves may be found.
            </p>
            <p>
                <uc:source runat="server" src="scookies.aspx.cs" converttabs="true" showheader="false" initiallyvisible="true" />
            </p>
        </div>
        <h3><span runat="server" id="xml" class="header">XML</span></h3>
        <div runat="server" id="xmlContainer">
            <uc:source runat="server" src="scookies.xml" showheader="false" initiallyvisible="true" />
        </div>
        <h3><span runat="server" id="webform" class="header">ASP.NET WebForm</span></h3>
        <div runat="server" id="webformContainer">
            <uc:source runat="server" src="scookies.aspx" converttabs="true" showheader="false" initiallyvisible="true" />
        </div>
    </div>
</asp:Content>