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

控件的公共属性介绍完后,接下来是控件事件的处理,事件处理分为服务器事件和客户端事件处理,对于服务器端事件,除了在内部处理删除和上传的按钮点击事件外,组件还公开一个OnAfterOperation事件,当上传成功时或删除成功是触发该事件,而根据传给事件command名称可以判断是删除还是上传成功.之所以设置此事件,是考虑到今后的开发的filepicker作准备,其界面如下
filepicker.jpg
当点击图片按钮时如下:
Popup.jpg
当该弹出窗口的上传下载组件每次操作成功后均应刷新预览窗口,因此设置此服务器端事件.

以下是具体的服务器端事件代码
  

ContractedBlock.gif ExpandedBlockStart.gif server event #region server event
InBlock.gif
//处理删除按钮事件
InBlock.gif
  private void Delete_Clicked(Object sender, System.Web.UI.ImageClickEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if(this.FileName!=string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.IO.File.Delete(
this.Page.MapPath(this.FullFileName));
InBlock.gif    
this.MessageBox("删除操作成功!");
InBlock.gif    
this.FileName = string.Empty;
InBlock.gif
//成功时触发事件
InBlock.gif
    OnAfterOperation(new CommandEventArgs("Delete",null));
ExpandedSubBlockEnd.gif   }

InBlock.gif   
ExpandedSubBlockEnd.gif  }

InBlock.gif
//当操作成功时的弹出信息框
InBlock.gif

InBlock.gif  
private void MessageBox(string strInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   strInfo
="<script>alert('"+strInfo+"')</script>";
InBlock.gif   
if(this.Page != null)
InBlock.gif    
this.Page.RegisterStartupScript(System.Guid.NewGuid().ToString(),strInfo);
ExpandedSubBlockEnd.gif  }

InBlock.gif
//初始化时将扩展名均化为小写字符便于今后验证
InBlock.gif
  protected override void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
base.OnInit (e);
InBlock.gif   
foreach(StringItem item in this.ExtFilters)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    item.Text 
= item.Text.ToLower();
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif
//上传按钮事件
InBlock.gif
  private void Upload_Clicked(Object sender, System.Web.UI.ImageClickEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   System.Web.HttpPostedFile file 
= this.FileUpload.PostedFile;
InBlock.gif   
string ExtName = file.FileName.Substring(file.FileName.LastIndexOf("."+ 1);
InBlock.gif   
InBlock.gif   
if(file.ContentLength > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif
//服务器端文件类型验证
InBlock.gif
    StringItem item = new StringItem();
InBlock.gif    item.Text 
= ExtName.ToLower();
InBlock.gif    
if((this.ExtFilters.Count > 0)&&(!this.ExtFilters.Contains(item)))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
this.MessageBox("文件类型不支持无法上传!");
InBlock.gif     
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
switch(this.UploadFileNameType)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
case FileNameType.AutoGenerate:
InBlock.gif
//此时用guid的唯一性自动生成文件名
InBlock.gif
      this.FileName = System.Guid.NewGuid().ToString() + "." + ExtName;
InBlock.gif      
break;
InBlock.gif     
case FileNameType.ClientSide:
InBlock.gif
//获取客户端文件名
InBlock.gif
      this.FileName = file.FileName.Substring(file.FileName.LastIndexOf("\\")+1);
InBlock.gif      
break;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
if(this.FileName!=string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     file.SaveAs(
this.Page.MapPath(this.FullFileName));
InBlock.gif     
this.MessageBox("上传操作成功!");
InBlock.gif
//成功时触发事件
InBlock.gif
     OnAfterOperation(new CommandEventArgs("Upload",null));
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
//事件键
InBlock.gif
  private static readonly object EventOP = new object();
InBlock.gif  
public event CommandEventHandler  AfterOperation
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   add
dot.gif{ Events.AddHandler(EventOP,value); }
ExpandedSubBlockStart.gifContractedSubBlock.gif   remove
dot.gif{ Events.RemoveHandler(EventOP,value); }
ExpandedSubBlockEnd.gif  }

InBlock.gif  
protected virtual void OnAfterOperation( CommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   CommandEventHandler OpHandler 
= (CommandEventHandler)Events[EventOP];
InBlock.gif   
if( OpHandler != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    OpHandler(
this,e);
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif  
#endregion

None.gif




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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值