⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tabdriventemplatedwebcontrol.cs

📁 这是一个简单的论坛程序源码
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Caching;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Globalization;
using System.Xml.Serialization;
using NetFocus.Web.Core;

namespace NetFocus.Web.Applications.Forum
{
    public abstract class TabDrivenTemplatedWebControl : UserControl
    {
        #region Public Properties

        /// <summary>
        /// Returns the currently selected tab
        /// </summary>
        public virtual string Selected
        {
            get
            {
                return (string)ViewState["Selected"];
            }
            set
            {
                ViewState["Selected"] = value;
            }
        }

        /// <summary>
        /// returns the location of the current tab configuration file
        /// </summary>
        public virtual string FileLocation
        {
            get
            {
                string path = Context.Server.MapPath(this.ResolveUrl(FileName));
                return path;
            }
        }

        /// <summary>
        /// returns the file name containing the tab configuration
        /// </summary>
        public virtual string FileName
        {
            get
            {
                Object state = ViewState["FileName"];
                if (state != null)
                {
                    return (String)state;
                }
                return "tabs.config";
            }
            set
            {
                ViewState["FileName"] = value;
            }
        }

        /// <summary>
        /// if true, the url is written into the control label
        /// </summary>
        public virtual bool UseDirectNavigation
        {
            get
            {
                Object state = ViewState["UseDirectNavigation"];
                if (state != null)
                {
                    return (bool)state;
                }
                return false;
            }
            set
            {
                ViewState["UseDirectNavigation"] = value;
            }
        }
        #endregion

        #region GetTabs()
        /// <summary>
        /// Returns the current instance of the TabCollection
        /// </summary>
        /// <returns></returns>
        protected TabCollection GetTabs()
        {
            string path = FileLocation;

            if (path.StartsWith("/") || path.StartsWith("~"))
                path = Context.Server.MapPath(path);

            TabCollection tc = Caches.Get(path) as TabCollection;
            if (tc == null)
            {
                tc = (TabCollection)Serializer.ConvertFileToObject(path, typeof(TabCollection));
                //Caches.Max(path, tc, new CacheDependency(path));
            }
            TabCollection filterTabs = new TabCollection();
            System.Collections.Generic.List<Tab> tabs = new System.Collections.Generic.List<Tab>();
            foreach (Tab tab in tc.Tabs)
            {
                if (tab.IsValid(WebContext.Current.User))
                {
                    tabs.Add(tab);
                }
            }
            filterTabs.Tabs = tabs.ToArray();
            return filterTabs;
        }
        #endregion

        #region Tab Helpers

        /// <summary>
        /// Resolves the current url and attempts to append the specified querystring
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public string FormatLink(Tab t)
        {
            string url = null;

            if (t.UrlName == null)
            {
                if (!t.HasHref)
                    return null;

                if (t.HasQueryString)
                    url = string.Format(t.Href.Replace('^', '&'), Context.Request.QueryString[t.QueryString]);
                else
                    url = t.Href;
            }
            else
            {
                if (!t.HasParameters)
                    url = FormatUrl(t.UrlName);
                else
                    url = FormatUrl(t.UrlName, t.Parameters);
            }

            return ResolveUrl(url);
        }

        public string FormatUrl(string name)
        {
            return SiteUrls.Instance().FormatUrl(name);
        }

        public string FormatUrl(string name, params object[] parameters)
        {
            //将navbar.config配置文件中的参数的值转换为相应的数据
            if (parameters != null)
            {
                for (int i = 0; i < parameters.Length; i++)
                {
                    string paramaterValue = parameters[i] as string;
                    if (!string.IsNullOrEmpty(paramaterValue))
                    {
                        if (paramaterValue == "$UserID")
                        {
                            parameters[i] = WebContext.Current.User.UserId.ToString();
                        }
                        else if (paramaterValue == "$UserName")
                        {
                            parameters[i] = WebContext.Current.User.UserName;
                        }
                    }
                }
            }
            return SiteUrls.Instance().FormatUrl(name, parameters);
        }

        public virtual string GetText(Tab t)
        {
            if (t.HasText)
            {
                return t.Text;
            }
            else
            {
                if (string.IsNullOrEmpty(t.ResourceFile))
                {
                    return Resources.GetString(t.ResourceName);
                }
                else
                {
                    return Resources.GetString(t.ResourceName, t.ResourceFile);
                }
            }
        }
        #endregion

        #region GetState
        /// <summary>
        /// Walks the tab and it's children to see if any of them are currently selected
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        protected SelectedState GetState(Tab t)
        {
            //Check the parent
            if (string.Compare(t.Name, Selected, true, CultureInfo.InvariantCulture) == 0)
                return SelectedState.Selected;

            //Walk each of the child tabs
            if (t.HasChildren)
            {
                foreach (Tab child in t.Children)
                {
                    if (string.Compare(child.Name, this.Selected, true, CultureInfo.InvariantCulture) == 0)
                        return SelectedState.ChildSelected;

                    else if (child.HasChildren)
                    {
                        foreach (Tab cc in child.Children)
                            if (string.Compare(cc.Name, this.Selected, true, CultureInfo.InvariantCulture) == 0)
                                return SelectedState.ChildSelected;
                    }
                }
            }

            //Nothing here is selected
            return SelectedState.Not;
        }

        #endregion

        #region SelectedState
        /// <summary>
        /// Internal enum used to track if a tab is selected
        /// </summary>
        protected enum SelectedState
        {
            Not,
            Selected,
            ChildSelected
        };
        #endregion
    }

    #region TabCollection
    /// <summary>
    /// TabCollection is a container for all of the tabs.
    /// </summary>
    [Serializable]
    public class TabCollection
    {
        private Tab[] _tabs;
        /// <summary>
        /// Property Tabs (Tab[])
        /// </summary>
        [XmlArray("Tabs")]
        public Tab[] Tabs
        {
            get { return this._tabs; }
            set { this._tabs = value; }
        }
    }
    #endregion

    #region Tab

    /// <summary>
    /// Tab is a container object which represents a singe tab
    /// </summary>
    [Serializable]
    public class Tab
    {
        #region Private Members
        private string _text;
        private string _href;
        private string _name;
        private string _target = "_self";
        private string _queryString;
        private string _roles;
        private string _imagename = null;
        private string _resourcename = null;
        private string _resourcefile = null;
        private string _urlname = null;
        private string[] _urlparameters = null;
        private bool _enable = true;
        private Tab[] _children;
        private bool _isRoot = false;
        private string _filter = null;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -