论Web控件开发 - 完美上传下载控件“新”(二)

在Upload的最开始是一连串的常量声明,因为考虑到所有的ViewState中的属性最终都要在页面的一个inputhidden控件中序列化,因此我在这定义了这些常量,目的就是想减少序例化的字节数(因为现在键只有一个字节),以此来提高响应速度,
  

ContractedBlock.gif ExpandedBlockStart.gif const #region const
InBlock.gif  
protected const string AllowDeleteKey = "A";//是否显示删除按钮
InBlock.gif
  protected const string WidthKey = "C";//控件宽度
InBlock.gif
  protected const string CSSKey = "D";//控件中textbox的css类
InBlock.gif
  protected const string FilePathKey = "E";//上传文件的路径
InBlock.gif
  protected const string UploadAltKey = "F";//上传按钮上的提示
InBlock.gif
  protected const string DeleteAltKey = "G";//删除按钮上的提示
InBlock.gif
  protected const string DownloadAltKey = "H";//下载按钮上的提示
InBlock.gif
  protected const string UploadImgKey = "I";//上传按钮上的图片
InBlock.gif
  protected const string DeleteImgKey = "J";//删除按钮上的图片
InBlock.gif
  protected const string DownloadImgKey = "K";//下载按钮上的图片
InBlock.gif
  protected const string UploadFileNameTypeKey = "L";//上传文件命名类型
InBlock.gif
  protected const string DefaultImagePath = "/kss_client/upload/";//缺省图片路径
ExpandedBlockEnd.gif
  #endregion


接下来是内部的子控件,前面我们说过,这个控件中一共涉及到四个控件:

 

ContractedBlock.gif ExpandedBlockStart.gif child controls #region child controls
InBlock.gif
//上传文件控件,用来选择
InBlock.gif
  protected System.Web.UI.HtmlControls.HtmlInputFile _fileUpload; 
InBlock.gif  
protected System.Web.UI.HtmlControls.HtmlInputFile FileUpload
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
this.EnsureChildControls();//确保调用子控件时已经创建了该控件
InBlock.gif
    return this._fileUpload;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
//隐藏input控件,用来支持验证器控件,保存文件名
InBlock.gif
  protected System.Web.UI.HtmlControls.HtmlInputHidden _txtFileName;
InBlock.gif  
protected System.Web.UI.HtmlControls.HtmlInputHidden TxtFileName
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
this.EnsureChildControls();
InBlock.gif    
return this._txtFileName;
InBlock.gif
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
//上传按钮
InBlock.gif
  protected ImageButton _btnUpLoad;
InBlock.gif  
protected ImageButton BtnUpLoad
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
this.EnsureChildControls();
InBlock.gif    
return this._btnUpLoad;
InBlock.gif
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
//删除按钮
InBlock.gif
  protected ImageButton _btnDelete;
InBlock.gif  
protected ImageButton BtnDelete
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
this.EnsureChildControls();
InBlock.gif    
return this._btnDelete;
InBlock.gif
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
//下载文件超链接
InBlock.gif
  protected System.Web.UI.WebControls.HyperLink _btnDownload;
InBlock.gif  
protected System.Web.UI.WebControls.HyperLink BtnDownload
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
this.EnsureChildControls();
InBlock.gif    
return this._btnDownload;
InBlock.gif
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif  
#endregion

None.gif

接下来是属性的声明

 

ContractedBlock.gif ExpandedBlockStart.gif   public field #region public field
InBlock.gif
InBlock.gif  [
InBlock.gif  Bindable(
true),
InBlock.gif  Category(
"Appearance"),
InBlock.gif  DefaultValue(
true),
InBlock.gif  NotifyParentProperty(
true), 
InBlock.gif  ]
InBlock.gif
//是否显示删除按钮
InBlock.gif
  public bool AllowDelete 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
object o = ViewState[AllowDeleteKey];
InBlock.gif    
return (o == null)?true:(bool)o;
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    ViewState[AllowDeleteKey] 
= value; 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif
InBlock.gif  [
InBlock.gif  Bindable(
true),
InBlock.gif  Category(
"Appearance"),
InBlock.gif  DefaultValue(
typeof(System.Web.UI.WebControls.Unit),""),
InBlock.gif  NotifyParentProperty(
true),
InBlock.gif  TypeConverter(
typeof(System.Web.UI.WebControls.UnitConverter))
InBlock.gif  ]
InBlock.gif
//控件宽度
InBlock.gif
  public System.Web.UI.WebControls.Unit Width
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
object o = ViewState[WidthKey];
InBlock.gif    
return(o==null)? System.Web.UI.WebControls.Unit.Empty:(System.Web.UI.WebControls.Unit)o;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
set
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    ViewState[WidthKey] 
= value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
//  protected const string CSSKey = "D";
InBlock.gif  
//  protected const string FilePathKey = "E";
InBlock.gif
  [
InBlock.gif  Bindable(
true),
InBlock.gif  Category(
"Appearance"),
InBlock.gif  DefaultValue(
"")
InBlock.gif  ]
InBlock.gif
//控件cssClass
InBlock.gif
  public string CSS 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
object o = ViewState[CSSKey];
InBlock.gif    
return (o == null)?string.Empty:(string)o;
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    ViewState[CSSKey] 
= value; 
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }
 
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif  
ExtFilters#region ExtFilters
InBlock.gif  
private StringItemCollection _extFilters;
InBlock.gif  [
InBlock.gif  NotifyParentProperty(
true),
InBlock.gif  PersistenceMode(PersistenceMode.InnerProperty),
InBlock.gif  DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
InBlock.gif  ]
InBlock.gif
//有效扩展名,其中StringItemCollection 类是支持视图状态的字符串数组类
InBlock.gif
  public StringItemCollection ExtFilters
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if(_extFilters==null
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     _extFilters 
= new StringItemCollection();
InBlock.gif     
if(this.IsTrackingViewState)
InBlock.gif      _extFilters.TrackViewState();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return _extFilters;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  
#endregion

InBlock.gif
InBlock.gif  [
InBlock.gif  Bindable(
true),
InBlock.gif  Category(
"Appearance"),
InBlock.gif  DefaultValue(
"/")
InBlock.gif  ]
InBlock.gif
//上传的文件路径
InBlock.gif
  public string FilePath 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
object o = ViewState[FilePathKey];
InBlock.gif    
return (o == null)?"/":(string)o;
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
string v = value.Replace("\\","/");
InBlock.gif    
if((v!=string.Empty)&&(!v.EndsWith("/")))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     v 
= v.Trim() + "/";
ExpandedSubBlockEnd.gif    }

InBlock.gif    ViewState[FilePathKey] 
= v; 
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  [
InBlock.gif  Bindable(
true),
InBlock.gif  Category(
"Appearance"),
InBlock.gif  DefaultValue(
"上传文件")
InBlock.gif  ]
InBlock.gif
//上传按钮提示
InBlock.gif
  public string UploadAlt 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
object o = ViewState[UploadAltKey];
InBlock.gif    
return (o == null)?"上传文件":(string)o;
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    ViewState[UploadAltKey] 
= value; 
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif  [
InBlock.gif  Bindable(
true),
InBlock.gif  Category(
"Appearance"),
InBlock.gif  DefaultValue(
"删除文件")
InBlock.gif  ]
InBlock.gif
//删除按钮提示
InBlock.gif
  public string DeleteAlt 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
object o = ViewState[DeleteAltKey];
InBlock.gif    
return (o == null)?"删除文件":(string)o;
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    ViewState[DeleteAltKey] 
= value; 
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  [
InBlock.gif  Bindable(
true),
InBlock.gif  Category(
"Appearance"),
InBlock.gif  DefaultValue(
"下载文件")
InBlock.gif  ]
InBlock.gif
//下载按钮提示
InBlock.gif
  public string DownloadAlt
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
object o = ViewState[DownloadAltKey];
InBlock.gif    
return (o == null)?"下载文件":(string)o;
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    ViewState[DownloadAltKey] 
= value; 
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif  [ 
InBlock.gif  Editor(
typeof(ImageUrlEditor),typeof(UITypeEditor)),
InBlock.gif  NotifyParentProperty(
true),
InBlock.gif  Category(
"Appearance"), 
InBlock.gif  DefaultValue(DefaultImagePath 
+ "btnUpload.gif")
InBlock.gif  ] 
InBlock.gif
//上传按钮图片
InBlock.gif
  public string UploadImg
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
object o = ViewState[UploadImgKey];
InBlock.gif    
return(o==null)? DefaultImagePath + "btnUpload.gif":(string)o;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
set
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    ViewState[UploadImgKey] 
= value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  [ 
InBlock.gif  Editor(
typeof(ImageUrlEditor),typeof(UITypeEditor)),
InBlock.gif  NotifyParentProperty(
true),
InBlock.gif  Category(
"Appearance"), 
InBlock.gif  DefaultValue(DefaultImagePath 
+ "btnDelete.gif")
InBlock.gif  ] 
InBlock.gif
//删除按钮图片
InBlock.gif
  public string DeleteImg
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
object o = ViewState[DeleteImgKey];
InBlock.gif    
return(o==null)? DefaultImagePath + "btnDelete.gif":(string)o;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
set
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    ViewState[DeleteImgKey] 
= value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  [ 
InBlock.gif  Editor(
typeof(ImageUrlEditor),typeof(UITypeEditor)),
InBlock.gif  NotifyParentProperty(
true),
InBlock.gif  Category(
"Appearance"), 
InBlock.gif  DefaultValue(DefaultImagePath 
+ "btnDownload.gif")
InBlock.gif  ] 
InBlock.gif
//下载按钮图片
InBlock.gif
  public string DownloadImg
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
object o = ViewState[DownloadImgKey];
InBlock.gif    
return(o==null)? DefaultImagePath + "btnDownload.gif":(string)o;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
set
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    ViewState[DownloadImgKey] 
= value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif  [ 
InBlock.gif  NotifyParentProperty(
true),
InBlock.gif  Category(
"Appearance"), 
InBlock.gif  DefaultValue(
typeof(FileNameType),"ClientSide")
InBlock.gif  ] 
InBlock.gif
//上传文件命名类型
InBlock.gif
  public FileNameType UploadFileNameType
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
object o = ViewState[UploadFileNameTypeKey];
InBlock.gif    
return(o==null)? FileNameType.ClientSide:(FileNameType)o;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
set
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    ViewState[UploadFileNameTypeKey] 
= value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif  [
InBlock.gif  Bindable(
true),
InBlock.gif  Category(
"Appearance"),
InBlock.gif  DefaultValue(
"")
InBlock.gif  ]
InBlock.gif
//文件名称
InBlock.gif
  public string FileName 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
return this.TxtFileName.Value;
InBlock.gif
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
this.TxtFileName.Value = value; 
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif 
InBlock.gif  [
InBlock.gif  DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
InBlock.gif  Browsable(
false),
InBlock.gif  ]
InBlock.gif
//包含路径的全文件名称
InBlock.gif
  public string FullFileName
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
get dot.gif{return this.FilePath + this.FileName;}
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
this.FileName = value.Substring(value.LastIndexOf("/")+1);
InBlock.gif    
this.FilePath = value.Substring(0,value.LastIndexOf("/")+1);
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif  [
InBlock.gif  DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
InBlock.gif  Browsable(
false),
InBlock.gif  ]
InBlock.gif
//文件大小
InBlock.gif
  public long FileSize
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if(this.FileName == string.Empty)
InBlock.gif     
return 0;
InBlock.gif    System.IO.FileInfo inf 
= new System.IO.FileInfo(this.Page.MapPath(this.FullFileName));
InBlock.gif    
if(inf.Exists)
InBlock.gif     
return inf.Length;
InBlock.gif    
else
InBlock.gif     
return 0;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif  
#endregion

None.gif


转载于:https://www.cnblogs.com/keyss/archive/2005/01/22/95803.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值