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

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

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

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

   
//delete btn
   if(this.AllowDelete) WriteSpace(writer); 
//绑定delete客户端事件
   onClick = (this.BtnDelete.Attributes["onclick"]==null)?string.Empty:this.BtnDelete.Attributes["onclick"];
   
this.BtnDelete.Attributes["onclick"= string.Format("if(!({0}_obj.CheckDelete())) return false;",this.ClientID)+ onClick;
   
this.BtnDelete.RenderControl(writer);


   
//download btn
   WriteSpace(writer);
//绑定download客户端事件
   this.BtnDownload.Attributes["onclick"]=string.Format("if(!({0}_obj.CheckDownload(this))) return false;",this.ClientID);
   
this.BtnDownload.RenderControl(writer);
   writer.RenderEndTag();
   
   writer.RenderEndTag();
   writer.RenderEndTag();
  }

  
  
protected override void CreateChildControls()
  
{
   
this.Controls.Clear();
   
//button hidden ctl0首先创建inputhidden因为其它控件会用到些控件.
   this._txtFileName = new System.Web.UI.HtmlControls.HtmlInputHidden();
   
this.Controls.Add(this._txtFileName);

   
//file upload ctl1创建上传文件控件
   this._fileUpload = new System.Web.UI.HtmlControls.HtmlInputFile();
//充满第一个单元格
   this._fileUpload.Style.Add("WIDTH","100%");
//应用样式
   if(this.CSS!=string.Empty)
    
this._fileUpload.Attributes.Add("class",this.CSS);
   
this.Controls.Add(this._fileUpload);

   
//button upload上传控件
   this._btnUpLoad = new ImageButton();
   
this._btnUpLoad.ImageUrl = this.UploadImg;
   
if(this.UploadFileNameType != FileNameType.AutoGenerate)
    
this._btnUpLoad.EnableWarning = true;
   
else
    
this._btnUpLoad.EnableWarning = false;
   
this._btnUpLoad.AlternateText = this.UploadAlt;
   
this._btnUpLoad.WarningMessage = "如果文件已经存在将被覆盖,确定吗?";
   
this._btnUpLoad.CausesValidation = false;
//绑定上传事件
   this._btnUpLoad.Click += new ImageClickEventHandler(Upload_Clicked);
   
this.Controls.Add(this._btnUpLoad);

   
//button delete创建删除按钮
   this._btnDelete = new ImageButton();
   
this._btnDelete.ImageUrl = this.DeleteImg;
   
this._btnDelete.EnableWarning = true;
   
this._btnDelete.AlternateText = this.DeleteAlt;
   
this._btnDelete.WarningMessage = "确定要删除吗?";
//不触发验证
   this._btnDelete.CausesValidation = false;
//绑定删除事件
   this._btnDelete.Click +=new ImageClickEventHandler(Delete_Clicked);
   
if(!this.AllowDelete)
    
this._btnDelete.Visible = false;
   
this.Controls.Add(this._btnDelete);

   
//button download
   this._btnDownload = new HyperLink();
   
this._btnDownload.Style.Add("cusor","hand");
   
this._btnDownload.ImageUrl = this.DownloadImg;
   
this._btnDownload.ToolTip = this.DownloadAlt;
   
this._btnDownload.NavigateUrl = this.FullFileName;
   
this._btnDownload.Target = "_blank";
   
this._btnDownload.Text = "download";

   
this.Controls.Add(this._btnDownload);


//子控件创建成功
   ChildControlsCreated = true;
  }


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

        }

        
protected override object SaveViewState()
        
{
            
object[] savedState = new object[2];
            savedState[
0= base.SaveViewState ();
            savedState[
1= this.ExtFilters.SaveViewState();
        
            
for(int i=0;i<savedState.Length;i++)
            
{
                
if(savedState[i] != null)
                    
return savedState;
            }

            
return null;
        }

        
protected override void TrackViewState()
        
{
            
base.TrackViewState();
            
this.ExtFilters.TrackViewState();
        }