欢迎光临
我们一直在努力

一个"简单"的ASP.NET的服务器控件

using system;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: TagPrefix("EndWell""EW")]
namespace EndWell
{
    [DefaultProperty(
"Text")]
    [ToolboxData(
"<{0}:HyperlinkFileList runat="server">")]
    [ToolboxBitmap(
"HyperlinkFileList.ico")]
    
    
public class HyperlinkFileList : Panel
    {
        [Bindable(
true)]
        [Category(
"files List")]
        [Description(
"The Title of the list of files")]
        
public string FilesTitle {getset;}

        [Bindable(true)]
        [Category(
"Files List")]     
        [Description(
"The directory of the files to list:  (~/Files/)")]
        
// these two built in editors were lacking:
        
//[EditorAttribute(typeof(System.Web.UI.Design.UrlEditor), typeof(UITypeEditor))]
        
//[EditorAttribute(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(UITypeEditor))]
        [EditorAttribute(typeof(EndWell.DualModeFolderEditor), typeof(UITypeEditor))]  
        
public string FilesDirectory { getset; }

        [Bindable(true)]
        [Category(
"Files List")]
        [Description(
"The filter for the files to show:  (*.*)")]
        
public string FilesFilter { getset; }

        [Bindable(true)]
        [Category(
"Files List")]
        [Description(
"Text to show when there are no files")]
        
public string NoFilesText { getset; }

        // ---- Private vars --------------------------

        
private String[] m_FilesArray;  // cached for performance

        // ---- Default constants-------------------

        
const String DEF_FILES_DIR = "~/xml/";
        
const String DEF_FILES_FILT = "*.xml";
        
const String DEF_FILES_TITLE = "XML Files:";
        
const String DEF_NOFILES_TEXT = "";

        // ---- Constructor -------------------------- 

        
public HyperlinkFileList()
        {
            
// set defaults for our properties
            FilesDirectory = DEF_FILES_DIR;
            FilesFilter 
= DEF_FILES_FILT;
            FilesTitle 
= DEF_FILES_TITLE;
            NoFilesText 
= DEF_NOFILES_TEXT;

            // Set defaults for panel properties
            
// I don't like the default width to be full screen
            
// And a border looks better
            Width = new Unit("300px");
            BorderStyle 
= BorderStyle.Solid;
            BorderWidth 
= 1;
            BorderColor 
= Color.Black;

            // If height is set, force scroll bars to keep list
            
// from spilling over the panel/div boundaries.
            if ((Height != null&& (ScrollBars == ScrollBars.None))
                ScrollBars 
= ScrollBars.Auto;
           
            
// Allow multiple controls to be placed horizontally
            
// (normally each div get's its own line)           
            Style["display"= "inline-block";

            // add spacing outside the control
            Style["margin"= "0.5em";

            // add space inside the control           
            Style["padding-left"= "0.5em";
            Style[
"padding-right"= "0.5em";
            Style[
"padding-bottom"= "0.5em";
            
// top space usually comes from the title...
            if (String.IsNullOrEmpty(FilesTitle) == true)
                Style[
"padding-top"= "0.5em";
        }

        // ---- RenderContents ----------------------------
        
//
        
// Spit out the HTML
 
        
protected override void RenderContents(HtmlTextWriter Output)
        {
            
// output the title if one was set
            if (String.IsNullOrEmpty(FilesTitle) == false)
            {
                Output.Write(
"<h3>  ");  // cosmetic spacing
                Output.Write(FilesTitle);
                Output.Write(
"</h3>");
            }

            GetFilesArray();
  
            if (m_FilesArray.Length == 0)
            {
                Output.Write(HttpUtility.HtmlEncode(NoFilesText));              
            }
            
else
            {
                
foreach (String OneFile in m_FilesArray)
                {
                    HyperLink Link 
= new HyperLink();
                    Link.NavigateUrl 
= Path.Combine(FilesDirectory, Path.GetFileName(OneFile));
                    Link.Text 
= Path.GetFileNameWithoutExtension(OneFile);                  
                    Link.RenderControl(Output);
                    Output.WriteBreak();
                }
            }
        }

        // ---- GetFilesArray -------------------------
        
//
        
// Fill the m_FilesArray with a list of files
        
// either from disk or the cache

        
private void GetFilesArray()
        {
            
// see if the file list is in the cache.
            
// use directory and filter as unique key
            m_FilesArray = Page.Cache[FilesDirectory + FilesFilter] as String[];

            if (m_FilesArray != null)
                
return;

            // if no files filter set, use the default one.
            if (String.IsNullOrEmpty(FilesFilter))
                FilesFilter 
= DEF_FILES_FILT;

            // if no files directory set, use the default one.
            if (String.IsNullOrEmpty(FilesDirectory))
                FilesDirectory 
= DEF_FILES_DIR;

            // if a virtual path is detected, map to full path
            String FullPath;
            
if (FilesDirectory.StartsWith("~"))
                FullPath 
= Context.Server.MapPath(FilesDirectory);
            
else
                FullPath 
= FilesDirectory;

            // get the files
            m_FilesArray = Directory.GetFiles(FullPath, FilesFilter, SearchOption.TopDirectoryOnly);
            
            
// put the list in the cache so we don't have to read the disk again
            
// use a dependency on the directory being read from for auto refreshing
            Page.Cache.Insert(FilesDirectory + FilesFilter,   // unique key
                              m_FilesArray,                   // list of files to store
                              new CacheDependency(FullPath)); // dependency on directory
           
        }
    }
}

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《一个"简单"的ASP.NET的服务器控件》
文章链接:https://www.456zj.com/614.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址