asp.net组件设计总结

1 . 继承control类:

一般不可见html元素主要是继承control类,比如meta中的元素或者隐藏域等

2 .继承webcontrol类:

一般可见元素比如
< input type = text > 等,为了获取更多的样式支持,减少代码量,一般直接继承webcontrol:

比如分页控件等:

http:
// blog.csdn.net/46539492/archive/2008/04/02/2244627.aspx

3 .复合控件:

定义复合控件一般要继承INamingContainer这个接口。比如一个简单用户登陆控件

http:
// blog.csdn.net/46539492/archive/2008/03/14/2182934.aspx

4 .数据列表控件:

比如DropDownList,CheckBoxList等控件一般继承ListControl,又因为这些控件存在数据传输或者说数据回发,则要继承IPostBackDataHandler接口。

 

5 .复合列表控件

比如CheckBoxList,RadioButtonList等控件,除了继承ListControl类,IPostBackDataHandler接口外,还要实现IRepeatInfoUser接口,IRepeatInfoUser接口定义了重复项列表的列表控件实现的属性和方法,最后还因为他们是复合控件,还要继承INamingContainer接口6.数据绑定控件

 

(
1 )DataList

DataList类似于CheckBoxList,是重复项列表的列表控件,所以需要继承IRepeatInfoUser,也是复合控件,所以继承INamingContainer接口,此外还要继承BaseDataList类,该类用数据列表控件的基类,如DataList和DataGrid,该类提供所有数据列表控件的所有方法和属性。

(
2 )Repeater

Repeater比较灵活,继承control和INamingContainer接口。

(
3 )DataGrid

DataGrid类,和DataList类似,主要继承BaseDataList类和INamingContainer接口

(
4 )GridView
GridView是比较复杂的数据绑定控件,继承CompositeDataBoundControl, IPostBackContainer, IPostBackEventHandler, ICallbackContainer, ICallbackEventHandler

其中CompositeDataBoundControl表示由其他服务器控件组成的表格数据绑定控件的基类; IPostBackContainer接口定义一个方法;使控件能够获取客户端脚本选项。ICallbackContainer接口定义一个方法,使控件能够获取回调脚本;ICallbackEventHandler用于指示控件可以作为服务器的回调事件的目标。 

6 .导航控件

(
1 )TreeView控件

TreeView控件主要继承HierarchicalDataBoundControl, IPostBackEventHandler, IPostBackDataHandler, ICallbackEventHandler,其中HierarchicalDataBoundControl用作所有 ASP.NET 
2.0  版数据绑定控件的基类,这些控件以分层形式显示它们的数据。因为有数据回发所以继承IPostBackDataHandler,继承IPostBackEventHandler处理回发事件,和GridView类似,ICallbackEventHandler用于指示控件可以作为服务器的回调事件的目标

(
2 )Menu控件

Menu主要继承HierarchicalDataBoundControl, IPostBackEventHandler, INamingContainer,因为Menu是个复合控件,继承INamingContainer接口。

二、样式相关

1 . 重写TagKey

示例如下:

       重写TagKey
重写TagKey

默认是HtmlTextWriterTag.Span,当需要其它标签时,需要重写TagKey,

比如以下代码:

 

 
< table width = " 100% "  style = "  height:20px; border:solid 1px #cccccc; "  cellpadding = " 0 "  cellspacing = " 0 "   >

            
< tr >

                
< td align = " right " >

                   当前第

                    
< asp:Label ID = " lblCurrentPage "  runat = " server " ></ asp:Label > 页,

                    总共
< asp:Label ID = " lblRecodeCount "  runat = " server " ></ asp:Label > 条纪录,

                    共
< asp:Label ID = " lblPageCount "  runat = " server " ></ asp:Label > 页,

                    
< asp:Label ID = " Label1 "  runat = " server " ></ asp:Label >

                    
< asp:LinkButton ID = " lnkbtnFrist "  runat = " server "  OnClick = " lnkbtnFrist_Click " >< font face = webdings color = " red " > 9 </ font ></ asp:LinkButton >

                     
< asp:LinkButton ID = " lnkbtnPre "  runat = " server "  OnClick = " lnkbtnPre_Click " >< font face = webdings color = " red " > 7 </ font ></ asp:LinkButton >

                     
< asp:LinkButton ID = " lnkbtnNext "  runat = " server "  OnClick = " lnkbtnNext_Click " >< font face = webdings color = " red " > 8 </ font ></ asp:LinkButton >

                    
< asp:LinkButton ID = " lnkbtnLast "  runat = " server "  OnClick = " lnkbtnLast_Click " >< font face = webdings color = " red " > : </ font ></ asp:LinkButton >

                    跳转到第
< asp:TextBox ID = " txtPageIndex "  runat = " server "  style = " width:40px; "  onkeypress = " myKeyDown(); " ></ asp:TextBox > < asp:Button ID = " BtnChangePage "  runat = " server "  Text = " GO "  OnClick = " BtnChangePage_Click "   />                    

              
</ td >

            
</ tr >

        
</ table >


这段代码要写成控件,就需要重写TaGKey,改为HtmlTextWriterTag.Table。

然后绘制控件:

 

绘制控件
绘制控件
    }
}

2 .重写CreateControlStyle

重写CreateControlStyle,可以根据需要应用相关的样式,比如继承Label类,并重写CreateControlStyle方法,使该控件具有表格的样式:

 

protected   override  Style CreateControlStyle()
        ...
{

            
return new TableStyle(ViewState);
        }


        [BrowsableAttribute(
true )]
        [DescriptionAttribute(
" 网格线 " )]
        [CategoryAttribute(
" Appearance " )]
        
public   virtual  GridLines GridLines
        ...
{
            
get ...return ((TableStyle)ControlStyle).GridLines; }
            
set ...{ ((TableStyle)ControlStyle).GridLines = value; }
        }

这样该自定义的label就具有表格的样式,比如网格线。

3 .继承webcontrol

通过继承webcontrol类可以获得webcontrol类很多样式,比如:



4 .通过继承Style类来自定义Style

示例如下:

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Web;
using  System.ComponentModel;
using  System.Web.UI.WebControls;
using  System.Web.UI;
namespace  MyLabel
...
{
    
public class LabelStyle:Style
    ...
{
        
public LabelStyle() ...{ }
        
public LabelStyle(StateBag viewState) : base(viewState) ...{ }

        
public virtual String ImageUrl
        ...
{
            
get ...return ViewState["imageUrl"!= null ? (string)ViewState["imageUrl"] : ""; }
            
set ...{ ViewState["imageUrl"= value; }
        }

        
//判断视图状态是否为空
        internal bool IsSet(string key)
        ...
{
            
return ViewState[key] != null;
        }


        
/**//**/
        
/**//// <summary>
        
/// 是否定义样式元素
        
/// </summary>

        public override bool IsEmpty
        ...
{
            
get
            ...
{
                
return base.IsEmpty && !IsSet("imageUrl");
            }

        }

        
public override void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)
        ...
{
            writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, ImageUrl);


            
base.AddAttributesToRender(writer, owner);
        }


    }

}


使用方法:

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.ComponentModel;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;

namespace  MyLabel
...
{
    
public class MyLabel3:Label
    ...
{
        
protected override Style CreateControlStyle()
        ...
{
            
return new LabelStyle();//使用自定义的Style
        }


        [DefaultValue(
"")]
        
public virtual string ImageUrl
        ...
{
            
get
            ...
{
                
return ((LabelStyle)ControlStyle).ImageUrl;
            }

            
set
            ...
{
                ((LabelStyle)ControlStyle).ImageUrl 
= value;
            }


        }

    }

}


 

三、属性相关

1 .简单属性

简单属性是指属性值可以很容易转换为字符串表达式的属性,这种属性的值通常为Boolean、Byte、Char、Double、Enum、Int32、DateTime等简单数值类型,以及String类型和枚举类型。开发人员可以通过添加代码,将简单属性存储在ViewState字典中,以在回发间进行状态管理。

通常的示例如下:

 

 [Browsable(
true )]
        [Category(
" Appearance " )]
        [Description(
" 设置单击当前行时当前行的背景色 " )]
        
public   virtual  Color ClickBackGroundColor
        ...
{
            
get
            ...
{
                
return ViewState["ClickBackGroundColor"]!=null?(Color)ViewState["ClickBackGroundColor"]:Color.Empty;
            }

            
set
            ...
{

                ViewState[
"ClickBackGroundColor"= value;
            }

        }

2 .复杂属性

如果一个属性的类型是本身具有属性(称为子属性)的类,则该属性就称为复杂属性。例如,WebControl类的Font属性的类型是本身具有属性(如Bold和Name)的FontInfo类。Bold和Name是WebControl的Font属性的子属性。

复杂属性不能简单的用ViewState来进行状态管理,需要自定义视图状态,比如我前一篇文章《千万级数据分页之二
--- 一个简单的自定义分页控件》定义的ButtonStyle,TextBoxStyle等就属于复杂属性:

 

    样式属性
样式属性

        自定义视图状态
自定义视图状态

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值