创建一个自定义WebControl控件 WebImageButton的过程

题目:创建一个自定义WebControl控件 WebImageButton的过程

 关键字:WebControl,ImageButton,EventArgs

 关键字:WebControl,ImageButton,EventArgs

 简要:WebImageButton 继承于 System.Web.UI.WebControls.WebControl,该控件具有WebControl的公共属性、方法和事件,同时可以在这些基础上进行功能扩充。本例就是在WebControl的基础上创建其它Web控件,构成一个复合控件。

 简要:WebImageButton 继承于 System.Web.UI.WebControls.WebControl,该控件具有WebControl的公共属性、方法和事件,同时可以在这些基础上进行功能扩充。本例就是在WebControl的基础上创建其它Web控件,构成一个复合控件。

 内容:

 内容:

 重载CreateChildControls方法,添加我们自己的控件

 重载CreateChildControls方法,添加我们自己的控件

protected override void CreateChildControls()

              {

//清除创建的控件               

Controls.Clear();

//清除视图状态

                     ClearChildViewState();

//创建controls

                     CreateCtrls();

               }

 下面是CreateCtrls()的实现方法:

 下面是CreateCtrls()的实现方法:

private void CreateCtrls()

              {

                     ScritpBlock();

                  this.Attributes ["style"]=this.Style.ToString();

                     System.Web.UI.WebControls.ImageButton   image =new System.Web.UI.WebControls.ImageButton();

                      

                     if(ImageStyle !=string.Empty )

                     {

                            image.Attributes["Style"]=ImageStyle ;

                     }

                     image.ImageUrl =ImageUrl ;

                     image.Attributes["style"]=string.Format("filter:alpha(opacity={0})",Alpha );//style="filter:alpha(opacity=20)"

                     image.ImageAlign =ImageAlign.AbsMiddle ;

                     image.ToolTip =this.ToolTip;

                    

                     LinkButton button=new LinkButton ();

                     if(OnClickScript.Trim()!="") //附加执行脚本程序的功能

                     {

                            image.Attributes["onclick"] =OnClickScript ;

                            button.Attributes["onclick"]= OnClickScript ;

                     }

             this.Attributes["onmouseover"]="imagebuttonmouseover(this)";

             this.Attributes["onmouseover"]="imagebuttonmouseover(this)";

                     this.Attributes["onmouseout"]="imagebuttonmouseout(this)";

                     image.Attributes["onmouseover"]="imagebuttonmouseover(this)";

                     image.Attributes["onmouseout"]="imagebuttonmouseout(this)";

                      button.Text = this.Text ;

                    

                     this.Attributes["style"]=string.Format("filter:alpha(opacity={0})",Alpha );

                     this.ToolTip =this.ToolTip ;

             

                      image.Click+=new ImageClickEventHandler(image_Click);

                     button.Click +=new EventHandler(button_Click);

    if(Direction ==方向.横向 )

                     {

                            BuildHorizontal(image,button);

                     }

                     else

                     {

                            BuildVertical (image,button);

                     }

          }

从上面我们创建了一个ImageButton控件和一个Button控件,这两个控件各自有自己的事件,如何和我们的WebImageButton事件进行挂钩呢,那么我们就需要对WebImageButton控件创建一个新的事件。方法如下:

那我们先定义一个事件的类,

public class ImageButtonEventArgs :EventArgs

       {

              public string ImageUrl;

              public string Text;

       }

接着在WebImageButton类中注册该事件

#region Event

              public delegate void ImageButtonClickEventHandler(object sender,ImageButtonEventArgs e);

              public event ImageButtonClickEventHandler ImageButtonClick;

              public virtual void OnImageButtonClick(ImageButtonEventArgs e)

              {

                     if(ImageButtonClick!=null){

                            ImageButtonClick(this,e);

                     }

              }

             

             

              #endregion

最后我们再在WebImageButton类的image_Click事件中触发该事件,这样的话就将复合控件中的控件和整个控件的事件关联起来了。

private void image_Click(object sender, ImageClickEventArgs e)

              {

                    ImageButtonEventArgs et=new ImageButtonEventArgs();

                     et.Text =this.Text;

                     et.ImageUrl =imageUrl ;

        OnImageButtonClick(et);

        OnImageButtonClick(et);

                     this.DataBind();

              }

到此控件创建完成,最后一步重载Render方法,实现编辑状态下效果

到此控件创建完成,最后一步重载Render方法,实现编辑状态下效果

protected override void Render(HtmlTextWriter output)

              {

                    if (Site != null && Site.DesignMode)

                            CreateChildControls();

                     base.Render(output);

              }

技术中国   提供代码下载

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值