动态构建菜单类

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Collections;

namespace yd
{
    /// <summary>
    /// 创建菜单
    /// </summary>
    public class Menu
    {
        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="tab"></param>
        public Menu(Table tab)
        {
            this._tab = tab;

            //初始化表及其他对象
            this.InitInfo();
        }


        #region 定义变量
        /// <summary>
        /// 页面对象
        /// </summary>
        private Page _page = null;
        /// <summary>
        /// 页面对象
        /// </summary>
        private Page page
        {
            get
            {
                return (this._page);
            }
        }

        /// <summary>
        /// 菜单存放的表格对象
        /// </summary>
        private Table _tab = null;
        /// <summary>
        /// 菜单存放的表格对象
        /// </summary>
        private Table tab
        {
            get
            {
                return (this._tab);
            }
        }

        /// <summary>
        /// 当前存储参数数据表
        /// </summary>
        private Hashtable _dt = null;
        /// <summary>
        /// 当前存储参数数据表
        /// </summary>
        private Hashtable dt
        {
            get { return (this._dt); }
            set { this._dt = value; }
        }

        /// <summary>
        /// 是否显示帮助按钮,默认为是
        /// </summary>
        private bool _IsHelp = true;
        /// <summary>
        /// 是否显示帮助按钮
        /// </summary>
        public bool IsHelp
        {
            get { return (this._IsHelp); }
            set { this._IsHelp = value; }
        }

        /// <summary>
        /// 是否显示关闭按钮,默认为否
        /// </summary>
        private bool _IsClose = false;
        /// <summary>
        /// 是否显示关闭按钮
        /// </summary>
        public bool IsClose
        {
            get { return (this._IsClose); }
            set { this._IsClose = value; }
        }


        /// <summary>
        /// 上一个页面的返回地址
        /// </summary>
        private string _ToBackPageUrl = string.Empty;
        /// <summary>
        /// 上一个页面的返回地址
        /// </summary>
        public string ToBackPageUrl
        {
            get
            {
                if (this._ToBackPageUrl == string.Empty)
                {
                    try
                    {
                        if (System.Web.HttpContext.Current.Request.UrlReferrer.PathAndQuery != null)
                        {
                            this._ToBackPageUrl = System.Web.HttpContext.Current.Request.UrlReferrer.PathAndQuery;
                        }
                    }
                    catch
                    {
                        this._ToBackPageUrl = "";
                    }
                }

                return (this._ToBackPageUrl);
            }
            set { this._ToBackPageUrl = value; }
        }

        /// <summary>
        /// 菜单标题信息
        /// </summary>
        private string _Title = string.Empty;
        /// <summary>
        /// 菜单标题信息
        /// </summary>
        public string Title
        {
            get { return (this._Title); }
            set { this._Title = value; }
        }

        #endregion 结束定义属性

        /// <summary>
        /// 初始化当前对象
        /// </summary>
        private void InitInfo()
        {
            //初始化表格
            this.tab.CssClass = "ydMenuCssParentTable";
            this.tab.Width = Unit.Percentage(100);
            //this.tab.BorderWidth = Unit.Pixel(0);
            this.tab.Height = Unit.Pixel(25);
            this.tab.CellPadding = 0;
            this.tab.CellSpacing = 0;

            //初始化数据表
            this._dt = new Hashtable();
        }


        /// <summary>
        /// 菜单对象
        /// </summary>
        public class MenuItem
        {
            #region 定义属性
            /// <summary>
            /// 当前菜单对象文本值
            /// </summary>
            private string _Text = string.Empty;
            /// <summary>
            /// 当前菜单对象文本值
            /// </summary>
            public string Text
            {
                get { return (this._Text); }
                set { this._Text = value; }
            }

            /// <summary>
            /// 当前菜单指向的onclick客户端脚本
            /// </summary>
            private string _OnClickScript = string.Empty;
            /// <summary>
            /// 当前菜单指向的onclick客户端脚本
            /// </summary>
            public string OnClickScript
            {
                get { return (this._OnClickScript); }
                set { this._OnClickScript = value; }
            }

            /// <summary>
            /// 菜单指向链接地址
            /// </summary>
            private string _Url = string.Empty;
            /// <summary>
            /// 菜单指向链接地址
            /// </summary>
            public string Url
            {
                get { return (this._Url); }
                set { this._Url = value; }
            }

            /// <summary>
            /// 当前菜单对象提示值
            /// </summary>
            private string _ToolTip = string.Empty;
            /// <summary>
            /// 当前菜单对象提示值
            /// </summary>
            public string ToolTip
            {
                get { return (this._ToolTip); }
                set { this._ToolTip = value; }
            }


            /// <summary>
            /// 当前菜单是否选中,默认为否
            /// </summary>
            private bool _Checked = false;
            /// <summary>
            /// 当前菜单是否选中,默认为否
            /// </summary>
            public bool Checked
            {
                get { return (this._Checked); }
                set { this._Checked = value; }
            }

            /// <summary>
            /// 当前菜单是否不可操作,默认为否
            /// </summary>
            private bool _Disabled = false;
            /// <summary>
            /// 当前菜单是否不可操作,默认为否
            /// </summary>
            public bool Disabled
            {
                get { return (this._Disabled); }
                set { this._Disabled = value; }
            }


            /// <summary>
            /// 当前菜单可否显示,默认为是
            /// </summary>
            private bool _Visible = true;
            /// <summary>
            /// 当前菜单可否显示,默认为是
            /// </summary>
            public bool Visible
            {
                get { return (this._Visible); }
                set { this._Visible = value; }
            }
            #endregion 定义属性

            /// <summary>
            /// 构造器
            /// </summary>
            public MenuItem()
            {
                this._Text = Text;
            }

            /// <summary>
            /// 构造器
            /// </summary>
            /// <param name="Text">菜单名称</param>
            public MenuItem(string Text)
            {
                this._Text = Text;
            }
        }

        /// <summary>
        /// 添加新菜单
        /// </summary>
        /// <param name="Item">菜单对象</param>
        public void Add(yd.Menu.MenuItem Item)
        {
            this.AddAt(this.dt.Count, Item);
        }


        /// <summary>
        /// 添加新菜单
        /// </summary>
        /// <param name="pos">要添加在哪个位置</param>
        /// <param name="Item">菜单对象</param>
        public void AddAt(int pos, yd.Menu.MenuItem Item)
        {
            //添加行
            this.dt.Add(pos, Item);
        }

        /// <summary>
        /// 添加一个空行,以避免顶天情况
        /// </summary>
        private void AddTopRow()
        {
            TableRow r = new TableRow();
            TableCell c = new TableCell();
            c.Height = Unit.Pixel(3);
            r.Controls.Add(c);
            this.tab.Controls.Add(r);
        }

        /// <summary>
        /// 开始加载菜单
        /// </summary>
        /// <returns></returns>
        public bool Load()
        {
            try
            {
                this.AddTopRow();
                //添加一个标题行
                TableRow r = new TableRow();
                //将行添加到表格中
                this.tab.Controls.Add(r);

                r.Controls.Add(this.GetTitleCell());

                //生成一个新单元格
                TableCell contentCell = new TableCell();
                contentCell.HorizontalAlign = HorizontalAlign.Right;
                r.Controls.Add(contentCell);

                //Label Label = new Label();
                //Label.Attributes.Add("style", "filter:shadow(color=#cccccc,direction=120);padding-right:;padding-bottom:3;");
                ////<div style="filter:shadow(color=#cccccc,direction=120);
                ////width:300;padding-right:10;padding-bottom:10">

                //在上述单元格里面再生成一个新表格,以存放各菜单
                Table mt = new Table();
                mt.CssClass = "ydMenuCss";
                mt.CellPadding = 1;
                mt.CellSpacing = 0;
                TableRow mtr = new TableRow();

                //生成常规按钮
                this.AddNormalMenu();

                //添加所记录的所有对象
                int icount = this.dt.Count;
                for (int i = 0; i < icount; i++)
                {
                    mtr.Cells.Add(this.AddTableCell((yd.Menu.MenuItem)this.dt[i]));
                }

                //将行添加到新表格中
                mt.Controls.Add(mtr);
                contentCell.Controls.Add(mt);

                return (true);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 生成标题行
        /// </summary>
        /// <returns></returns>
        private TableCell GetTitleCell()
        {
            //生成标题栏
            TableCell cTitle = new TableCell();
            cTitle.Wrap = false;
            cTitle.CssClass = "ydMenuCssParentLefttd";
            cTitle.Text = this.Title;

            return (cTitle);
        }

        /// <summary>
        /// 生成常规按钮
        /// </summary>
        private void AddNormalMenu()
        {
            //生成返回按钮
            if (this.ToBackPageUrl != string.Empty && this.ToBackPageUrl.Length > 0)
            {
                MenuItem backItem = new MenuItem();
                backItem.Text = "返回";
                backItem.ToolTip = "点击返回上一个页面";
                if (this.ToBackPageUrl != string.Empty)
                {
                    backItem.Url = this.ToBackPageUrl;
                }
                else
                {
                    backItem.Url = "#";
                    backItem.OnClickScript = "history.back();return(false);";
                }

                this.Add(backItem);
            }

            //生成关闭按钮
            if (this.IsClose == true)
            {
                MenuItem closeItem = new MenuItem();
                closeItem.Text = "关闭";
                closeItem.ToolTip = "点击关闭本页面";
                closeItem.Url = this.ToBackPageUrl;
                closeItem.OnClickScript = "self.close();return(false);";

                this.Add(closeItem);
            }

            //生成帮助菜单
            if (this.IsHelp == true)
            {
                System.Web.UI.Page p = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
                string s = p.AppRelativeVirtualPath;
                string pageUrl = p.Server.UrlEncode(s);
                string helpUrl = p.ResolveUrl("~/hlp/page/myhelp.aspx");

                MenuItem helpItem = new MenuItem();
                helpItem.Text = "帮助";
                helpItem.ToolTip = "点击查看本页面帮助信息";
                helpItem.OnClickScript = "return(openWindow(600,400,'" + helpUrl + "?mypage=" + pageUrl + "'));";

                this.Add(helpItem);
            }
        }

        /// <summary>
        /// 添加单元格
        /// </summary>
        /// <param name="Item"></param>
        private TableCell AddTableCell(yd.Menu.MenuItem Item)
        {
            TableCell c = new TableCell();
            c.Wrap = false;

            //生成链接对象
            HyperLink h = new HyperLink();

            h.NavigateUrl = (Item.Url == "" ? "#" : Item.Url);

            //选中效果
            if (Item.Checked == true)
            {
                h.CssClass = "currentmenu";
            }

            h.ToolTip = Item.ToolTip;

            //如果不可用,则显示效果
            if (Item.Disabled == true)
            {
                h.Attributes.Add("disabled", "true");
            }

            //执行脚本
            if (Item.OnClickScript != string.Empty && Item.OnClickScript.Length > 0)
            {
                h.Attributes.Add("onclick", Item.OnClickScript + "return(false);");
            }

            h.Visible = Item.Visible;

            //生成Label对象
            Label lab = new Label();
            lab.Text = Item.Text;
            if (Item.Checked == true)
            {
                lab.CssClass = "currentmenu";
            }

            h.Controls.Add(lab);
            c.Controls.Add(h);
            return (c);
        }

    }
}

posted @ 2010-04-11 19:07  夜色狼  阅读(161)  评论(0编辑  收藏  举报