当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > Asp.net2.0之自定义控件ImageButton

ASP.NET
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
.NET 常用功能和代码小结
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
asp.net IList查询数据后格式化数据再绑定控件
asp.net sql存储过程
asp.net 简单实现禁用或启用页面中的某一类型的控件
asp.net(c#)获取内容第一张图片地址的函数
The remote procedure call failed and did not execute的解决办法
ASP.NET 在线文件管理
asp.net 读取并修改config文件实现代码
ASP.NET Cookie 操作实现
asp.net Silverlight中的模式窗体
Silverlight中动态获取Web Service地址
asp.net Silverlight应用程序中获取载体aspx页面参数
asp.net 水晶报表隔行换色实现方法
asp.net 获取Gridview隐藏列的值
手动把asp.net的类生成dll文件的方法
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
动态指定任意类型的ObjectDataSource对象的查询参数
asp.net Md5的用法小结

ASP.NET 中的 Asp.net2.0之自定义控件ImageButton


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 85 ::
收藏到网摘: n/a

前言

上个星期三开始学自定义控件,做了不少练习。花了一上午时间写了一个imageButton,以前就像写这个控件,只是不会。

图片

正文

这个控件模仿winform中的button,可以支持图片和文字。可以选择执行服务器端程序还是客户端程序,还有一些简单的设置。

不足的是不支持样式,下次希望可以写一个工具条。

以下就是代码

以下为引用的内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
namespace ClassLibrary1
{
    [Serializable]
    
public class Picture
    {
       
        
private Unit height = 16;
        
        
private string src = string.Empty;
        [NotifyParentProperty(
true)]
        [Browsable(
true), Bindable(true), Description("图片路径"), Category("Appearance")]
        [Editor(
"System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"typeof(UITypeEditor))]
        
public string Src
        {
            
get { return this.src; }
            
set { this.src = value; }
        }
        [DefaultValue(
typeof(Unit),"16px") ]
        [NotifyParentProperty(
true)]
        
public Unit Height
        {
            
get { return height; }
            
set { height = value; }
        }
        
private Unit width = 16;
        [NotifyParentProperty(
true)]
        [DefaultValue(
typeof(Unit),"16px")]
        
public Unit Width
        {
            
get { return width; }
            
set { width = value; }
        }
    
       
public enum Align{Left ,Right  }

    }
    [Serializable] 
    
public class Label
    {
       
       
        
private string text = string.Empty;
        [NotifyParentProperty(
true)]
        
public string Text
        {
            
get { return text; }
            
set { text = value; }
        }
        
private System.Drawing.Font fontFamily=new System.Drawing.Font("宋体",8);

        
[NotifyParentProperty(
true)]
        
public System.Drawing.Font  Font
        {
            
get { return this.fontFamily; }
            
set { this.fontFamily = value; }
        }
         
    }
    [PersistChildren(
false )]
    [ParseChildren(
true)]
   
public  class ImageButton:Control ,INamingContainer,IPostBackEventHandler  
    {
        
public  enum RaiseEventType {Client,Server }
       
private Picture pic = new Picture();
       
private Picture.Align picAlign = Picture.Align.Left;
       
private Label label = new Label ();
       
private string jsFunction = string.Empty;
       
private static readonly object clickKey = new object();
       
public  enum TextAlign {Left ,Center,Right }
       [Browsable(
true), Bindable(true), Description("javascript方法"), Category("Action")]
       
public string JSFunction
       {
           
get { return this.jsFunction; }
           
set { this.jsFunction = value; }
       }

        
private  RaiseEventType  raiseEvent=RaiseEventType.Server ;
        [Browsable(
true), Bindable(true), Description("响应事件方式"), Category("Action")]
        
public RaiseEventType RaiseEvent
        {
            
get { return this.raiseEvent; }
            
set { this.raiseEvent = value; }
        }
        

       
private TextAlign align = TextAlign.Left;
       [Browsable(
true), Bindable(true), Description("文字的对齐方式"), Category("Appearance")]
       
public  TextAlign ALign
       {
           
get { return align; }
           
set { align = value; }

       }
       
private Unit width = 80;
       [Browsable(
true), Bindable(true), Description("控件宽度"), Category("Appearance")]
        [DefaultValue(
typeof(Unit),"80px") ]
       
public Unit Width
       {
           
get { return this.width; }
           
set { this.width = value; }
       }


        [Browsable(
true),Bindable(true),Category("Action")]
       
public event EventHandler OnClick
       {
           add
           {
               Events.AddHandler(clickKey ,value); 
           }
           remove
           {
               Events.RemoveHandler(clickKey ,value);
           }
       }
        [Browsable(
true), Bindable(true), Description("图片类"), Category("Appearance")]
        
public Picture.Align PicAlign
        {
            
get { return picAlign; }
            
set { picAlign = value; }
        }
     
       [Browsable(
true),Bindable(true),Description("图片类"),Category("Appearance")]
       [DesignerSerializationVisibility(DesignerSerializationVisibility.Content) ]
       [TypeConverter(
typeof(ExpandableObjectConverter))]
        [PersistenceMode(PersistenceMode.InnerProperty)]   
       
public Picture Pic
       {
           
get { return pic; }
           
       }
       [Browsable(
true),Bindable(true),Description("文字类"),Category("Appearance")]
       [DesignerSerializationVisibility(DesignerSerializationVisibility.Content) ]
       [TypeConverter(
typeof(ExpandableObjectConverter) )]
        [PersistenceMode(PersistenceMode.InnerProperty)]  
       
public Label Label
       {
           
get { return label; }
          
       }
       
protected override void Render(HtmlTextWriter writer)
       {
           
if (raiseEvent == RaiseEventType.Server)
           {
               writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.GetPostBackEventReference(
thisthis.ClientID));
           }
           
else
           {
               writer.AddAttribute(HtmlTextWriterAttribute.Onclick ,
"javascript:"+this.jsFunction); 
           }
         
           writer.AddStyleAttribute(HtmlTextWriterStyle.Cursor ,
"hand"); 
           writer.AddStyleAttribute(HtmlTextWriterStyle.Width,
this.width.Value.ToString() +"px");
           
if (align == TextAlign.Left)
           {
               writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign ,
"left" );
           }
           
else if (align == TextAlign.Center)
           {
               writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, 
"center");
           }
           
else
           {
               writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign ,
"right");
           }
        
           writer.RenderBeginTag(HtmlTextWriterTag.Div ); 
 
          
           
if (PicAlign == Picture.Align.Left)
           {
               AddPic(writer);
               AddLabel(writer);
           }
           
else
           {AddLabel(writer);
               AddPic(writer);
               
           }
           writer.RenderEndTag(); 
           
//base.Render(writer);
       }
       
private void AddPic(HtmlTextWriter writer)
       {
          
           writer.AddAttribute(HtmlTextWriterAttribute.Src,
base.ResolveClientUrl(pic.Src));
           writer.AddAttribute(HtmlTextWriterAttribute.Height ,pic.Height.ToString());
           writer.AddAttribute(HtmlTextWriterAttribute.Width ,pic.Width.ToString());
         
           writer.RenderBeginTag(HtmlTextWriterTag.Img);
           writer.RenderEndTag(); 
          
// writer.Write("<image src='"+this.Src+"' height="+pic.Height+" width="+pic.Width+" />"); 
       }
       
private void AddLabel(HtmlTextWriter writer)
       {
           writer.AddStyleAttribute(HtmlTextWriterStyle.VerticalAlign,
"middle");
           writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize ,label.Font.Size.ToString()
+"pt");
           writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily,label.Font.FontFamily.Name );
           
if(label.Font.Bold)
           {

           writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight,
"Bold" );
           }
           writer.RenderBeginTag(HtmlTextWriterTag.Label);

           writer.Write(label.Text
==string.Empty ?this.ClientID.ToString():label.Text);
 
          writer.RenderEndTag(); 
           
//writer.Write("<label>" + Label.Text + "</label>");
       }


       
#region IPostBackEventHandler 成员

       
public void RaisePostBackEvent(string eventArgument)
       {
           EventHandler e 
= (EventHandler)Events[clickKey];
           
if(e!=null)
           {
               e(
this,EventArgs.Empty );
           }
       }

       
#endregion

毕竟是刚学,肯定会有很多不足和错误,希望大家指正,谢谢。

结尾,继续努力,看完其它的资料。

原文地址:http://www.cnblogs.com/flyskyer/archive/2009/08/31/1557178.html