Click here to Skip to main content
15,884,917 members
Articles / Web Development / ASP.NET

AMenu - A Simple .NET Vertical Menu

Rate me:
Please Sign up or sign in to vote.
4.88/5 (26 votes)
8 Oct 2009CPOL4 min read 64K   3.1K   100  
A CSS based .NET vertical menu control.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;


namespace mtweb
{
  
//////////////////////////////////////////////////////////////////////////

public partial class DefaultPage : System.Web.UI.Page
{
  // Fields


  /////////////////////////////////////////////////////////
  // Events

  protected override void OnInit(EventArgs e)
  {
    base.OnInit(e);
    ScriptManager.GetCurrent(Page).Navigate += OnNavigate;

    // Append "?" to the default form action.
    // Needed to get AddHistoryPoint() working on Opera.
    Form1.Attributes["action"] = Request.FilePath + "?";
  }


  protected void Page_Load(object sender, EventArgs e)
  {
    // Load or reload a UserControl:
    string file = this.TrackedControl;

    if (!string.IsNullOrEmpty(file)) {
      if (IsPostBack) ReloadContent();
    }

    else {
      file = "~/Controls/Home.ascx";
      if (!IsPostBack) LoadContent(file, null);
      else throw new Exception("Page_Load(): invalid state");  // dbg
    }
  }


  // History event handler
  protected void OnNavigate(object sender, HistoryEventArgs e)
  {
    // Page
    string page = e.State["UCPage"];
    if (page == null) page = "~/Controls/Home.ascx";

    // Parameters
    ParameterCollection pc = null;
    string s = e.State["Params"];
    if (s != null) pc = Util.DeserializePC(s);

    // Load
    LoadContent(page, pc);
  }


  /////////////////////////////////////////////////////////
  // Properties

  protected string TrackedControl 
  {
    get {
      return ViewState["TrackedControl"] as string;
    }
    set {
      if (value == null) ViewState.Remove("TrackedControl");
      else ViewState["TrackedControl"] = value; 
    }
  }


  /////////////////////////////////////////////////////////
  // Content management

  // Load a new content
  public DControl LoadContent(string file, ParameterCollection pc)
  {
    // Remove all controls
    PlaceHolderAdminData.Controls.Clear();
    if (string.IsNullOrEmpty(file)) return null;
    
    // The return value
    DControl dc = null;

    // Load the control
    try {
      dc = DControl.LoadUC<DControl>(file, this);
      if (dc == null) throw new Exception();
    }
    catch (Exception ex) {
      string msg = "Couldn't load " + file + ". " + ex.Message;
      throw new Exception(msg);
    }

    // Save as tracked control
    TrackedControl = file;

    // Pass on parameters
    if (pc != null) dc.Params = pc;

    // Insert the new control
    PlaceHolderAdminData.Controls.Add(dc);

    // Add history points
    ScriptManager sm = ScriptManager.GetCurrent(Page);
    if (sm.IsInAsyncPostBack && !sm.IsNavigating)
    {
      sm.AddHistoryPoint("UCPage", file);
      sm.AddHistoryPoint("Params", Util.SerializePC(pc));
    }

    return dc;
  }


  // Reload content on postback
  public DControl ReloadContent()
  {
    // Get the tracked control
    string file = this.TrackedControl;

    // Error?
    if (string.IsNullOrEmpty(file)) {
      string msg = "Tracked control null or empty";
      throw new Exception(msg);
    }

    // The return value
    DControl dc = null;

    // Load the control
    try {
      dc = DControl.LoadUC<DControl>(file, this);
      if (dc == null) throw new Exception();
    }
    catch (Exception ex) {
      string msg = "Couldn't load " + file + ". " + ex.Message;
      throw new Exception(msg);
    }

    // Insert the new control
    PlaceHolderAdminData.Controls.Clear();
    PlaceHolderAdminData.Controls.Add(dc);

    return dc;
  }


  //
  public void CloseContent()
  {
    PlaceHolderAdminData.Controls.Clear();
    UpdatePanelAdminData.Update();
  }


  ///////////////////////////////////////////////
  // Click handlers

  protected void OnMenuClick(object sender, CommandEventArgs e)
  {
    string from = ((Control)sender).ClientID;
    string cmd = e.CommandName;
    string arg = e.CommandArgument as string;

    // Parameters for LoadContent()
    ParameterCollection pc = new ParameterCollection();
    pc.Add(new Parameter("CommandArgument", TypeCode.String, arg));

    if (!string.IsNullOrEmpty(cmd)) {
      LoadContent(cmd, pc);
    }
  }


  protected void ImageLogo_Click(object sender, ImageClickEventArgs e)
  {
    LoadContent("~/Controls/Home.ascx", null);
  }



  ///////////////////////////////////////////////
  // Excpetion handling

  // Pass on the errors from the UpdatePanels
  protected void OnAsyncPostbackError(object sender, AsyncPostBackErrorEventArgs e)
  {
    MethodInfo mi = typeof(Exception).GetMethod("InternalPreserveStackTrace",
                    BindingFlags.Instance | BindingFlags.NonPublic);

    mi.Invoke(e.Exception, null);
    throw e.Exception;
  }


}  // class
}  // namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Europe Europe
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions