对于客户端事件比较复杂,首先介绍一下客户端的脚本:
首先我在客户端声名一个KSS_Upload类的定义,它主要帮助我来实现客户端的验证
而当控件在生成期间通过以下代码实现,客户端upload对象的声明,其中参数{0}为控件的ID,{1}为扩展名验证器,{2}为上传文件的路径.
<SCRIPT language="JavaScript">
var {0}_obj = new KSS_Upload("{0}", {1}, "{2}");
</SCRIPT>
以下是服务器端客户事件相关代码
首先我在客户端声名一个KSS_Upload类的定义,它主要帮助我来实现客户端的验证
/************************************************************************/
/* Keyss.WebControls.Upload -- Version 1.0.0 */
/* Client Script for Internet Explorer 5.0 or Above */
/*----------------------------------------------------------------------*/
/* Date: July 25, 2004 */
/* Copyright: 2001-2004, www.keyss.cn. All Rights Reserved */
/* Email: keyss@21cn.com */
/************************************************************************/
function KSS_Upload(id, extFilter, path)
{
//对象ID
this.ID = id;
//上传文件路径
this.Path = (!path)?'':path;
//扩展名验证对象
this.ExtFilter = extFilter;
//当点击上传按钮时客户端验证函数
this.CheckUpload = function()
{
//this.ID + "__file"为htmlfileupload控件id
var fileName = document.getElementById(this.ID + "__file").value;
//验证fileupload中的文件名是否合法
if (!((fileName)&&(fileName!='')&&(fileName.match(/^([A-Za-z]:\\(.)+\\[^\\]+)$/))))
{
alert("文件名称格式有误!");
return false;
}
//是否对扩展名要求验证
if(!this.ExtFilter) return true;
if(!fileName.match(this.ExtFilter))
{
alert("文件种类不支持!");
return false;
}
return true;
}
//当点击删除按钮时客户端验证函数
this.CheckDelete = function()
{
//this.ID同时为inputhidden控件的id
var txtFileName = document.getElementById(this.ID);
var fileName = txtFileName.value;
//是否有删除的文件
if (!(fileName&&(fileName!='')))
{
alert("文件名称格式有误!");
return false;
}
return true;
}
//当下载时客户端验证函数
this.CheckDownload = function(obj)
{
//this.ID同时为inputhidden控件的id
var txtFileName = document.getElementById(this.ID);
var fileName = txtFileName.value;
var fullFileName = this.Path + fileName;
//是否有可下载的文件
if (!(fileName&&(fileName!='')))
{
alert("文件名称格式有误!");
return false;
}
obj.href = fullFileName;
return true;
}
}
/* Keyss.WebControls.Upload -- Version 1.0.0 */
/* Client Script for Internet Explorer 5.0 or Above */
/*----------------------------------------------------------------------*/
/* Date: July 25, 2004 */
/* Copyright: 2001-2004, www.keyss.cn. All Rights Reserved */
/* Email: keyss@21cn.com */
/************************************************************************/
function KSS_Upload(id, extFilter, path)
{
//对象ID
this.ID = id;
//上传文件路径
this.Path = (!path)?'':path;
//扩展名验证对象
this.ExtFilter = extFilter;
//当点击上传按钮时客户端验证函数
this.CheckUpload = function()
{
//this.ID + "__file"为htmlfileupload控件id
var fileName = document.getElementById(this.ID + "__file").value;
//验证fileupload中的文件名是否合法
if (!((fileName)&&(fileName!='')&&(fileName.match(/^([A-Za-z]:\\(.)+\\[^\\]+)$/))))
{
alert("文件名称格式有误!");
return false;
}
//是否对扩展名要求验证
if(!this.ExtFilter) return true;
if(!fileName.match(this.ExtFilter))
{
alert("文件种类不支持!");
return false;
}
return true;
}
//当点击删除按钮时客户端验证函数
this.CheckDelete = function()
{
//this.ID同时为inputhidden控件的id
var txtFileName = document.getElementById(this.ID);
var fileName = txtFileName.value;
//是否有删除的文件
if (!(fileName&&(fileName!='')))
{
alert("文件名称格式有误!");
return false;
}
return true;
}
//当下载时客户端验证函数
this.CheckDownload = function(obj)
{
//this.ID同时为inputhidden控件的id
var txtFileName = document.getElementById(this.ID);
var fileName = txtFileName.value;
var fullFileName = this.Path + fileName;
//是否有可下载的文件
if (!(fileName&&(fileName!='')))
{
alert("文件名称格式有误!");
return false;
}
obj.href = fullFileName;
return true;
}
}
而当控件在生成期间通过以下代码实现,客户端upload对象的声明,其中参数{0}为控件的ID,{1}为扩展名验证器,{2}为上传文件的路径.
<SCRIPT language="JavaScript">
var {0}_obj = new KSS_Upload("{0}", {1}, "{2}");
</SCRIPT>
以下是服务器端客户事件相关代码
//获得后缀名验证器字符串
protected virtual string GetExtValidateStr()
{
if(this.ExtFilters.Count > 0)
{
System.Text.StringBuilder st = new System.Text.StringBuilder();
st.Append("/(");
for(int i = 0; i<ExtFilters.Count;i++)
{
if(i != 0)
st.Append("|");
st.AppendFormat("(\\{0})",this.ExtFilters[i].Text.ToLower());
}
st.Append(")$/");
return st.ToString();
}
else
return "null";
}
static
protected override void OnPreRender(EventArgs e)
{
//注册KSS_Upload类
if(!Page.IsClientScriptBlockRegistered("Keyss.WebControls.Upload"))
{
Page.RegisterClientScriptBlock("Keyss.WebControls.Upload",Upload._webUploadScript);
}
//声明客户端jscript upload_obj对象
if(!Page.IsStartupScriptRegistered(this.ClientID))
{
string tmpStr = this.GetExtValidateStr();
Page.RegisterStartupScript(ClientID,string.Format(Upload._declareWebUploadScript,ClientID,tmpStr,this.FilePath));
}
}
protected virtual string GetExtValidateStr()
{
if(this.ExtFilters.Count > 0)
{
System.Text.StringBuilder st = new System.Text.StringBuilder();
st.Append("/(");
for(int i = 0; i<ExtFilters.Count;i++)
{
if(i != 0)
st.Append("|");
st.AppendFormat("(\\{0})",this.ExtFilters[i].Text.ToLower());
}
st.Append(")$/");
return st.ToString();
}
else
return "null";
}
static
protected override void OnPreRender(EventArgs e)
{
//注册KSS_Upload类
if(!Page.IsClientScriptBlockRegistered("Keyss.WebControls.Upload"))
{
Page.RegisterClientScriptBlock("Keyss.WebControls.Upload",Upload._webUploadScript);
}
//声明客户端jscript upload_obj对象
if(!Page.IsStartupScriptRegistered(this.ClientID))
{
string tmpStr = this.GetExtValidateStr();
Page.RegisterStartupScript(ClientID,string.Format(Upload._declareWebUploadScript,ClientID,tmpStr,this.FilePath));
}
}