在Upload的最开始是一连串的常量声明,因为考虑到所有的ViewState中的属性最终都要在页面的一个inputhidden控件中序列化,因此我在这定义了这些常量,目的就是想减少序例化的字节数(因为现在键只有一个字节),以此来提高响应速度,
const#region const
protected const string AllowDeleteKey = "A";//是否显示删除按钮
protected const string WidthKey = "C";//控件宽度
protected const string CSSKey = "D";//控件中textbox的css类
protected const string FilePathKey = "E";//上传文件的路径
protected const string UploadAltKey = "F";//上传按钮上的提示
protected const string DeleteAltKey = "G";//删除按钮上的提示
protected const string DownloadAltKey = "H";//下载按钮上的提示
protected const string UploadImgKey = "I";//上传按钮上的图片
protected const string DeleteImgKey = "J";//删除按钮上的图片
protected const string DownloadImgKey = "K";//下载按钮上的图片
protected const string UploadFileNameTypeKey = "L";//上传文件命名类型
protected const string DefaultImagePath = "/kss_client/upload/";//缺省图片路径
#endregion
protected const string AllowDeleteKey = "A";//是否显示删除按钮
protected const string WidthKey = "C";//控件宽度
protected const string CSSKey = "D";//控件中textbox的css类
protected const string FilePathKey = "E";//上传文件的路径
protected const string UploadAltKey = "F";//上传按钮上的提示
protected const string DeleteAltKey = "G";//删除按钮上的提示
protected const string DownloadAltKey = "H";//下载按钮上的提示
protected const string UploadImgKey = "I";//上传按钮上的图片
protected const string DeleteImgKey = "J";//删除按钮上的图片
protected const string DownloadImgKey = "K";//下载按钮上的图片
protected const string UploadFileNameTypeKey = "L";//上传文件命名类型
protected const string DefaultImagePath = "/kss_client/upload/";//缺省图片路径
#endregion
接下来是内部的子控件,前面我们说过,这个控件中一共涉及到四个控件:
child controls#region child controls
//上传文件控件,用来选择
protected System.Web.UI.HtmlControls.HtmlInputFile _fileUpload;
protected System.Web.UI.HtmlControls.HtmlInputFile FileUpload
{
get
{
this.EnsureChildControls();//确保调用子控件时已经创建了该控件
return this._fileUpload;
}
}
//隐藏input控件,用来支持验证器控件,保存文件名
protected System.Web.UI.HtmlControls.HtmlInputHidden _txtFileName;
protected System.Web.UI.HtmlControls.HtmlInputHidden TxtFileName
{
get
{
this.EnsureChildControls();
return this._txtFileName;
}
}
//上传按钮
protected ImageButton _btnUpLoad;
protected ImageButton BtnUpLoad
{
get
{
this.EnsureChildControls();
return this._btnUpLoad;
}
}
//删除按钮
protected ImageButton _btnDelete;
protected ImageButton BtnDelete
{
get
{
this.EnsureChildControls();
return this._btnDelete;
}
}
//下载文件超链接
protected System.Web.UI.WebControls.HyperLink _btnDownload;
protected System.Web.UI.WebControls.HyperLink BtnDownload
{
get
{
this.EnsureChildControls();
return this._btnDownload;
}
}
#endregion
//上传文件控件,用来选择
protected System.Web.UI.HtmlControls.HtmlInputFile _fileUpload;
protected System.Web.UI.HtmlControls.HtmlInputFile FileUpload
{
get
{
this.EnsureChildControls();//确保调用子控件时已经创建了该控件
return this._fileUpload;
}
}
//隐藏input控件,用来支持验证器控件,保存文件名
protected System.Web.UI.HtmlControls.HtmlInputHidden _txtFileName;
protected System.Web.UI.HtmlControls.HtmlInputHidden TxtFileName
{
get
{
this.EnsureChildControls();
return this._txtFileName;
}
}
//上传按钮
protected ImageButton _btnUpLoad;
protected ImageButton BtnUpLoad
{
get
{
this.EnsureChildControls();
return this._btnUpLoad;
}
}
//删除按钮
protected ImageButton _btnDelete;
protected ImageButton BtnDelete
{
get
{
this.EnsureChildControls();
return this._btnDelete;
}
}
//下载文件超链接
protected System.Web.UI.WebControls.HyperLink _btnDownload;
protected System.Web.UI.WebControls.HyperLink BtnDownload
{
get
{
this.EnsureChildControls();
return this._btnDownload;
}
}
#endregion
接下来是属性的声明
public field#region public field
[
Bindable(true),
Category("Appearance"),
DefaultValue(true),
NotifyParentProperty(true),
]
//是否显示删除按钮
public bool AllowDelete
{
get
{
object o = ViewState[AllowDeleteKey];
return (o == null)?true:(bool)o;
}
set
{
ViewState[AllowDeleteKey] = value;
}
}
[
Bindable(true),
Category("Appearance"),
DefaultValue(typeof(System.Web.UI.WebControls.Unit),""),
NotifyParentProperty(true),
TypeConverter(typeof(System.Web.UI.WebControls.UnitConverter))
]
//控件宽度
public System.Web.UI.WebControls.Unit Width
{
get
{
object o = ViewState[WidthKey];
return(o==null)? System.Web.UI.WebControls.Unit.Empty:(System.Web.UI.WebControls.Unit)o;
}
set
{
ViewState[WidthKey] = value;
}
}
// protected const string CSSKey = "D";
// protected const string FilePathKey = "E";
[
Bindable(true),
Category("Appearance"),
DefaultValue("")
]
//控件cssClass
public string CSS
{
get
{
object o = ViewState[CSSKey];
return (o == null)?string.Empty:(string)o;
}
set
{
ViewState[CSSKey] = value;
}
}
ExtFilters#region ExtFilters
private StringItemCollection _extFilters;
[
NotifyParentProperty(true),
PersistenceMode(PersistenceMode.InnerProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
]
//有效扩展名,其中StringItemCollection 类是支持视图状态的字符串数组类
public StringItemCollection ExtFilters
{
get
{
if(_extFilters==null)
{
_extFilters = new StringItemCollection();
if(this.IsTrackingViewState)
_extFilters.TrackViewState();
}
return _extFilters;
}
}
#endregion
[
Bindable(true),
Category("Appearance"),
DefaultValue("/")
]
//上传的文件路径
public string FilePath
{
get
{
object o = ViewState[FilePathKey];
return (o == null)?"/":(string)o;
}
set
{
string v = value.Replace("\\","/");
if((v!=string.Empty)&&(!v.EndsWith("/")))
{
v = v.Trim() + "/";
}
ViewState[FilePathKey] = v;
}
}
[
Bindable(true),
Category("Appearance"),
DefaultValue("上传文件")
]
//上传按钮提示
public string UploadAlt
{
get
{
object o = ViewState[UploadAltKey];
return (o == null)?"上传文件":(string)o;
}
set
{
ViewState[UploadAltKey] = value;
}
}
[
Bindable(true),
Category("Appearance"),
DefaultValue("删除文件")
]
//删除按钮提示
public string DeleteAlt
{
get
{
object o = ViewState[DeleteAltKey];
return (o == null)?"删除文件":(string)o;
}
set
{
ViewState[DeleteAltKey] = value;
}
}
[
Bindable(true),
Category("Appearance"),
DefaultValue("下载文件")
]
//下载按钮提示
public string DownloadAlt
{
get
{
object o = ViewState[DownloadAltKey];
return (o == null)?"下载文件":(string)o;
}
set
{
ViewState[DownloadAltKey] = value;
}
}
[
Editor(typeof(ImageUrlEditor),typeof(UITypeEditor)),
NotifyParentProperty(true),
Category("Appearance"),
DefaultValue(DefaultImagePath + "btnUpload.gif")
]
//上传按钮图片
public string UploadImg
{
get
{
object o = ViewState[UploadImgKey];
return(o==null)? DefaultImagePath + "btnUpload.gif":(string)o;
}
set
{
ViewState[UploadImgKey] = value;
}
}
[
Editor(typeof(ImageUrlEditor),typeof(UITypeEditor)),
NotifyParentProperty(true),
Category("Appearance"),
DefaultValue(DefaultImagePath + "btnDelete.gif")
]
//删除按钮图片
public string DeleteImg
{
get
{
object o = ViewState[DeleteImgKey];
return(o==null)? DefaultImagePath + "btnDelete.gif":(string)o;
}
set
{
ViewState[DeleteImgKey] = value;
}
}
[
Editor(typeof(ImageUrlEditor),typeof(UITypeEditor)),
NotifyParentProperty(true),
Category("Appearance"),
DefaultValue(DefaultImagePath + "btnDownload.gif")
]
//下载按钮图片
public string DownloadImg
{
get
{
object o = ViewState[DownloadImgKey];
return(o==null)? DefaultImagePath + "btnDownload.gif":(string)o;
}
set
{
ViewState[DownloadImgKey] = value;
}
}
[
NotifyParentProperty(true),
Category("Appearance"),
DefaultValue(typeof(FileNameType),"ClientSide")
]
//上传文件命名类型
public FileNameType UploadFileNameType
{
get
{
object o = ViewState[UploadFileNameTypeKey];
return(o==null)? FileNameType.ClientSide:(FileNameType)o;
}
set
{
ViewState[UploadFileNameTypeKey] = value;
}
}
[
Bindable(true),
Category("Appearance"),
DefaultValue("")
]
//文件名称
public string FileName
{
get
{
return this.TxtFileName.Value;
}
set
{
this.TxtFileName.Value = value;
}
}
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
Browsable(false),
]
//包含路径的全文件名称
public string FullFileName
{
get {return this.FilePath + this.FileName;}
set
{
this.FileName = value.Substring(value.LastIndexOf("/")+1);
this.FilePath = value.Substring(0,value.LastIndexOf("/")+1);
}
}
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
Browsable(false),
]
//文件大小
public long FileSize
{
get
{
if(this.FileName == string.Empty)
return 0;
System.IO.FileInfo inf = new System.IO.FileInfo(this.Page.MapPath(this.FullFileName));
if(inf.Exists)
return inf.Length;
else
return 0;
}
}
#endregion
[
Bindable(true),
Category("Appearance"),
DefaultValue(true),
NotifyParentProperty(true),
]
//是否显示删除按钮
public bool AllowDelete
{
get
{
object o = ViewState[AllowDeleteKey];
return (o == null)?true:(bool)o;
}
set
{
ViewState[AllowDeleteKey] = value;
}
}
[
Bindable(true),
Category("Appearance"),
DefaultValue(typeof(System.Web.UI.WebControls.Unit),""),
NotifyParentProperty(true),
TypeConverter(typeof(System.Web.UI.WebControls.UnitConverter))
]
//控件宽度
public System.Web.UI.WebControls.Unit Width
{
get
{
object o = ViewState[WidthKey];
return(o==null)? System.Web.UI.WebControls.Unit.Empty:(System.Web.UI.WebControls.Unit)o;
}
set
{
ViewState[WidthKey] = value;
}
}
// protected const string CSSKey = "D";
// protected const string FilePathKey = "E";
[
Bindable(true),
Category("Appearance"),
DefaultValue("")
]
//控件cssClass
public string CSS
{
get
{
object o = ViewState[CSSKey];
return (o == null)?string.Empty:(string)o;
}
set
{
ViewState[CSSKey] = value;
}
}
ExtFilters#region ExtFilters
private StringItemCollection _extFilters;
[
NotifyParentProperty(true),
PersistenceMode(PersistenceMode.InnerProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
]
//有效扩展名,其中StringItemCollection 类是支持视图状态的字符串数组类
public StringItemCollection ExtFilters
{
get
{
if(_extFilters==null)
{
_extFilters = new StringItemCollection();
if(this.IsTrackingViewState)
_extFilters.TrackViewState();
}
return _extFilters;
}
}
#endregion
[
Bindable(true),
Category("Appearance"),
DefaultValue("/")
]
//上传的文件路径
public string FilePath
{
get
{
object o = ViewState[FilePathKey];
return (o == null)?"/":(string)o;
}
set
{
string v = value.Replace("\\","/");
if((v!=string.Empty)&&(!v.EndsWith("/")))
{
v = v.Trim() + "/";
}
ViewState[FilePathKey] = v;
}
}
[
Bindable(true),
Category("Appearance"),
DefaultValue("上传文件")
]
//上传按钮提示
public string UploadAlt
{
get
{
object o = ViewState[UploadAltKey];
return (o == null)?"上传文件":(string)o;
}
set
{
ViewState[UploadAltKey] = value;
}
}
[
Bindable(true),
Category("Appearance"),
DefaultValue("删除文件")
]
//删除按钮提示
public string DeleteAlt
{
get
{
object o = ViewState[DeleteAltKey];
return (o == null)?"删除文件":(string)o;
}
set
{
ViewState[DeleteAltKey] = value;
}
}
[
Bindable(true),
Category("Appearance"),
DefaultValue("下载文件")
]
//下载按钮提示
public string DownloadAlt
{
get
{
object o = ViewState[DownloadAltKey];
return (o == null)?"下载文件":(string)o;
}
set
{
ViewState[DownloadAltKey] = value;
}
}
[
Editor(typeof(ImageUrlEditor),typeof(UITypeEditor)),
NotifyParentProperty(true),
Category("Appearance"),
DefaultValue(DefaultImagePath + "btnUpload.gif")
]
//上传按钮图片
public string UploadImg
{
get
{
object o = ViewState[UploadImgKey];
return(o==null)? DefaultImagePath + "btnUpload.gif":(string)o;
}
set
{
ViewState[UploadImgKey] = value;
}
}
[
Editor(typeof(ImageUrlEditor),typeof(UITypeEditor)),
NotifyParentProperty(true),
Category("Appearance"),
DefaultValue(DefaultImagePath + "btnDelete.gif")
]
//删除按钮图片
public string DeleteImg
{
get
{
object o = ViewState[DeleteImgKey];
return(o==null)? DefaultImagePath + "btnDelete.gif":(string)o;
}
set
{
ViewState[DeleteImgKey] = value;
}
}
[
Editor(typeof(ImageUrlEditor),typeof(UITypeEditor)),
NotifyParentProperty(true),
Category("Appearance"),
DefaultValue(DefaultImagePath + "btnDownload.gif")
]
//下载按钮图片
public string DownloadImg
{
get
{
object o = ViewState[DownloadImgKey];
return(o==null)? DefaultImagePath + "btnDownload.gif":(string)o;
}
set
{
ViewState[DownloadImgKey] = value;
}
}
[
NotifyParentProperty(true),
Category("Appearance"),
DefaultValue(typeof(FileNameType),"ClientSide")
]
//上传文件命名类型
public FileNameType UploadFileNameType
{
get
{
object o = ViewState[UploadFileNameTypeKey];
return(o==null)? FileNameType.ClientSide:(FileNameType)o;
}
set
{
ViewState[UploadFileNameTypeKey] = value;
}
}
[
Bindable(true),
Category("Appearance"),
DefaultValue("")
]
//文件名称
public string FileName
{
get
{
return this.TxtFileName.Value;
}
set
{
this.TxtFileName.Value = value;
}
}
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
Browsable(false),
]
//包含路径的全文件名称
public string FullFileName
{
get {return this.FilePath + this.FileName;}
set
{
this.FileName = value.Substring(value.LastIndexOf("/")+1);
this.FilePath = value.Substring(0,value.LastIndexOf("/")+1);
}
}
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
Browsable(false),
]
//文件大小
public long FileSize
{
get
{
if(this.FileName == string.Empty)
return 0;
System.IO.FileInfo inf = new System.IO.FileInfo(this.Page.MapPath(this.FullFileName));
if(inf.Exists)
return inf.Length;
else
return 0;
}
}
#endregion