C++ C++ C# C# ASP.NET Security ASP.NET Security ASM ASM Скачать Скачать Поиск Поиск Хостинг Хостинг  
  Программа для работы с LPT портом...
Язык: .NET — ©Alexey...
  "ASP.NET Atlas" – AJAX в исполнении Micro...
Язык: .NET — ©legigor@mail.ru...
  "Невытесняющая" Многопоточность...
Язык: C/C++ — ©...
  Update World C++: Сборник GPL QT исходников
  Весь сайт целиком можно загрузить по ссылкам из раздела Скачать
Дебетовая карта Home Credit [CPS] RU

 The lazy way to Menus / Custom Controls / ASP. NET

Introduction

Well, as a lazy programmer I'll always try to avoid writing more code than is necessary. So, when a friend of mine asked me to make a menu control for his website, I came up with this idea. Why not let .NET figure out how many ASPX pages the website has, and then create links for the pages automatically. Since the menu is a vertical sidebar menu obviously a DataGrid could be used. We also have the DirectoryInfo object to search the website for directories and files, and with these few tools and a few lines of code, the menu was in place. There is nothing fancy about the menu, but it is easy for you to make it snazzy and customize it to fit your needs. The demo project is just a website with some empty pages in different directories to demonstrate the power of this little menu control. To use the control just drag the menu onto every page you create.

And if you find this article helpful please do vote, since this is a good indication if I should post more articles about lazy programming.

On request, I have updated the code to work recursively, so now I take all the directories no matter which level. I didn't do it the first time because I never use more than two levels in my projects.

// Programmer: Jan Nielsen
// Date 07/08/05
namespace WebApplication1
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO;
    /// <summary>
    /// Summary description for Menu.
    /// </summary>
    public class Menu : System.Web.UI.UserControl
    {
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        protected DataTable links = new DataTable("links"); 
        protected DirectoryInfo info;
        private static string left = "0";
        private static string top = "0";
        public string Left
        {
            set{left = value;}
            get{return left;}
        }
        public string Top
        {
            set{top = value;}
            get{return top;}
        }
        private void Page_Load(object sender, System.EventArgs e)
        {
            links.Columns.Add("ButtonText");
            links.Columns.Add("Src");
            info = 
              new DirectoryInfo(Request.PhysicalApplicationPath);
            getDirs(info);
            DataGrid1.DataSource = links;
            DataGrid1.DataBind();
        }
        public void getDirs(DirectoryInfo info)
        {
            foreach(FileInfo i in info.GetFiles("*.aspx"))
            {
                // here we filter out Admin dir
                if(info.Name != "Admin") 
                {
                    // and here we filter out 
                    // Orders.aspx in Secure dir
                    if(i.Name != "Orders.aspx") 
                    {
                        string src = i.FullName.Replace(
                                         info.Root.ToString(),"");
                        src = src.Replace(@"\","/");
                        src = src.Remove(0,src.IndexOf('/'));
                        string[] r = new string[2];
                        r[0] = i.Name.Split('.')[0];
                        r[1] = src;
                        links.Rows.Add(r);
                    }
                }
            }
            if(info.GetDirectories().Length > 0)
            {
                foreach(DirectoryInfo d in info.GetDirectories())
                getDirs(d);
            }
        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by 
            // the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// Required method for Designer 
        /// support - do not modify
        /// the contents of this method 
        /// with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += 
              new System.EventHandler(this.Page_Load);
        }
        #endregion
    }
}



Дебетовая карта Home Credit [CPS] RU