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

服务器端事件和客户端事件介绍完后下面介绍控件的生成,

None.gif // 输出图片按钮之间的空白图片
None.gif
   protected   virtual   void  WriteSpace(HtmlTextWriter writer)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   writer.AddAttribute(HtmlTextWriterAttribute.Src, DefaultImagePath
+"HSpace2.gif");
InBlock.gif   writer.RenderBeginTag(HtmlTextWriterTag.Img);
InBlock.gif   writer.RenderEndTag();
ExpandedBlockEnd.gif  }

None.gif 
None.gif  
protected   override   void  Render(HtmlTextWriter writer)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif
//输出具有一行两个cell的表格其中第一个cell放置htmlinputfile,第二个cell放upload/delete/download/inputhidden按钮
InBlock.gif

InBlock.gif   writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, 
"0");
InBlock.gif   writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, 
"0");
InBlock.gif   
if(this.Width!=Unit.Empty)
InBlock.gif    writer.AddAttribute(HtmlTextWriterAttribute.Width, Width.ToString());
InBlock.gif   writer.AddAttribute(HtmlTextWriterAttribute.Border, 
"0");
InBlock.gif   
//table
InBlock.gif
   writer.RenderBeginTag(HtmlTextWriterTag.Table);
InBlock.gif   writer.RenderBeginTag(HtmlTextWriterTag.Tr);
InBlock.gif   
//cell1
InBlock.gif
//使第二cell的按钮充满单元格,由于inputfile的宽度为100%这样通过设置最外层table的width可以精确控制控件的宽度
InBlock.gif
   writer.AddAttribute(HtmlTextWriterAttribute.Width, "98%");
InBlock.gif   writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif
//定义inputfile的客户端id
InBlock.gif
   this.FileUpload.Attributes.Add("id",this.ClientID+"__file");
InBlock.gif   
this.FileUpload.RenderControl(writer);
InBlock.gif   writer.RenderEndTag();
InBlock.gif   
//cell2,为了使后三个按钮不换行输出nowrap属性
InBlock.gif
   writer.AddAttribute(HtmlTextWriterAttribute.Nowrap,"Yes");
InBlock.gif   writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif   
//hiden input file
InBlock.gif
   this.TxtFileName.Attributes.Add("id",this.ClientID);
InBlock.gif   
this.TxtFileName.RenderControl(writer);
InBlock.gif   
//upload btn
InBlock.gif
   WriteSpace(writer);
InBlock.gif
//绑定upload客户端事件
InBlock.gif
   string onClick = this.BtnUpLoad.Attributes["onclick"]==null?string.Empty:this.BtnUpLoad.Attributes["onclick"];
InBlock.gif   
this.BtnUpLoad.Attributes["onclick"= string.Format("if(!({0}_obj.CheckUpload())) return false;",this.ClientID) + onClick;
InBlock.gif   
this.BtnUpLoad.RenderControl(writer);
InBlock.gif
InBlock.gif   
//delete btn
InBlock.gif
   if(this.AllowDelete) WriteSpace(writer); 
InBlock.gif
//绑定delete客户端事件
InBlock.gif
   onClick = (this.BtnDelete.Attributes["onclick"]==null)?string.Empty:this.BtnDelete.Attributes["onclick"];
InBlock.gif   
this.BtnDelete.Attributes["onclick"= string.Format("if(!({0}_obj.CheckDelete())) return false;",this.ClientID)+ onClick;
InBlock.gif   
this.BtnDelete.RenderControl(writer);
InBlock.gif
InBlock.gif
InBlock.gif   
//download btn
InBlock.gif
   WriteSpace(writer);
InBlock.gif
//绑定download客户端事件
InBlock.gif
   this.BtnDownload.Attributes["onclick"]=string.Format("if(!({0}_obj.CheckDownload(this))) return false;",this.ClientID);
InBlock.gif   
this.BtnDownload.RenderControl(writer);
InBlock.gif   writer.RenderEndTag();
InBlock.gif   
InBlock.gif   writer.RenderEndTag();
InBlock.gif   writer.RenderEndTag();
ExpandedBlockEnd.gif  }

None.gif  
None.gif  
protected   override   void  CreateChildControls()
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
this.Controls.Clear();
InBlock.gif   
//button hidden ctl0首先创建inputhidden因为其它控件会用到些控件.
InBlock.gif
   this._txtFileName = new System.Web.UI.HtmlControls.HtmlInputHidden();
InBlock.gif   
this.Controls.Add(this._txtFileName);
InBlock.gif
InBlock.gif   
//file upload ctl1创建上传文件控件
InBlock.gif
   this._fileUpload = new System.Web.UI.HtmlControls.HtmlInputFile();
InBlock.gif
//充满第一个单元格
InBlock.gif
   this._fileUpload.Style.Add("WIDTH","100%");
InBlock.gif
//应用样式
InBlock.gif
   if(this.CSS!=string.Empty)
InBlock.gif    
this._fileUpload.Attributes.Add("class",this.CSS);
InBlock.gif   
this.Controls.Add(this._fileUpload);
InBlock.gif
InBlock.gif   
//button upload上传控件
InBlock.gif
   this._btnUpLoad = new ImageButton();
InBlock.gif   
this._btnUpLoad.ImageUrl = this.UploadImg;
InBlock.gif   
if(this.UploadFileNameType != FileNameType.AutoGenerate)
InBlock.gif    
this._btnUpLoad.EnableWarning = true;
InBlock.gif   
else
InBlock.gif    
this._btnUpLoad.EnableWarning = false;
InBlock.gif   
this._btnUpLoad.AlternateText = this.UploadAlt;
InBlock.gif   
this._btnUpLoad.WarningMessage = "如果文件已经存在将被覆盖,确定吗?";
InBlock.gif   
this._btnUpLoad.CausesValidation = false;
InBlock.gif
//绑定上传事件
InBlock.gif
   this._btnUpLoad.Click += new ImageClickEventHandler(Upload_Clicked);
InBlock.gif   
this.Controls.Add(this._btnUpLoad);
InBlock.gif
InBlock.gif   
//button delete创建删除按钮
InBlock.gif
   this._btnDelete = new ImageButton();
InBlock.gif   
this._btnDelete.ImageUrl = this.DeleteImg;
InBlock.gif   
this._btnDelete.EnableWarning = true;
InBlock.gif   
this._btnDelete.AlternateText = this.DeleteAlt;
InBlock.gif   
this._btnDelete.WarningMessage = "确定要删除吗?";
InBlock.gif
//不触发验证
InBlock.gif
   this._btnDelete.CausesValidation = false;
InBlock.gif
//绑定删除事件
InBlock.gif
   this._btnDelete.Click +=new ImageClickEventHandler(Delete_Clicked);
InBlock.gif   
if(!this.AllowDelete)
InBlock.gif    
this._btnDelete.Visible = false;
InBlock.gif   
this.Controls.Add(this._btnDelete);
InBlock.gif
InBlock.gif   
//button download
InBlock.gif
   this._btnDownload = new HyperLink();
InBlock.gif   
this._btnDownload.Style.Add("cusor","hand");
InBlock.gif   
this._btnDownload.ImageUrl = this.DownloadImg;
InBlock.gif   
this._btnDownload.ToolTip = this.DownloadAlt;
InBlock.gif   
this._btnDownload.NavigateUrl = this.FullFileName;
InBlock.gif   
this._btnDownload.Target = "_blank";
InBlock.gif   
this._btnDownload.Text = "download";
InBlock.gif
InBlock.gif   
this.Controls.Add(this._btnDownload);
InBlock.gif
InBlock.gif
InBlock.gif
//子控件创建成功
InBlock.gif
   ChildControlsCreated = true;
ExpandedBlockEnd.gif  }

None.gif
None.gif
最后是视图状态的相关函数,可以参考我以前的文章在此只是把代码列下
None.gif      protected   override   void  LoadViewState( object  state)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if(state !=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object[] savedState = (object[])state;
InBlock.gif                
if(savedState[0!= null)
InBlock.gif                    
base.LoadViewState(savedState[0]);
InBlock.gif                
if(savedState[1!= null)
InBlock.gif                    
this.ExtFilters.LoadViewState(savedState[1]);
ExpandedSubBlockEnd.gif            }
    
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif        
protected   override   object  SaveViewState()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
object[] savedState = new object[2];
InBlock.gif            savedState[
0= base.SaveViewState ();
InBlock.gif            savedState[
1= this.ExtFilters.SaveViewState();
InBlock.gif        
InBlock.gif            
for(int i=0;i<savedState.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(savedState[i] != null)
InBlock.gif                    
return savedState;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedBlockEnd.gif        }

None.gif        
protected   override   void  TrackViewState()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
base.TrackViewState();
InBlock.gif            
this.ExtFilters.TrackViewState();
ExpandedBlockEnd.gif        }


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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值