【Clingingboy】asp.net 简单介绍自定义控件简单属性和复杂属性

我们根据属性的不同表现形式,把其区分为简单属性和复杂属性

下面来看下属性的表现形式

简单属性表现形式如下,大家都很熟悉

None.gif < asp:TextBox  ID ="TextBox1"  Text ="textbox控件"  runat ="server" > asp:TextBox >

属性中含有子属性,称之为复杂对象,如Font属性
复杂属性的表现形式如下,

(1)连字符的表现形式
None.gif < asp:TextBox  ID ="TextBox1"  Text ="textbox控件"  runat ="server"  Font-Bold ="True" > asp:TextBox >

(2)内镶属性的表现形式,如定义样式

None.gif < asp:DataList  ID ="DataList1"  runat ="server" >
None.gif            
< SelectedItemStyle  />
None.gif            
< EditItemStyle  />
None.gif        
asp:DataList >

(3)内镶集合属性的表现形式,如DropDownList (先不介绍,大家可看MSDN)
None.gif < asp:DropDownList  ID ="DropDownList1"  runat ="server" >
None.gif            
< asp:ListItem > x asp:ListItem >
None.gif            
< asp:ListItem > xx asp:ListItem >
None.gif            
< asp:ListItem > xxx asp:ListItem >
None.gif        
asp:DropDownList >

下面得好好看

1,复杂属性基本使用方法

请看我是怎么做的,关于下面看到了一些 元数据,如果你不熟悉,请参考MSDN.

下面一段代码记录一个custom的信息.

1.1 定义枚举
None.gif using  System;
None.gif
None.gif
namespace  CustomComponents
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 
InBlock.gif    
/// 职业
ExpandedSubBlockEnd.gif    
/// 

InBlock.gif    public enum Metier
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        教师,程序员,作家
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


1.2定义复杂属性

None.gif using  System;
None.gif
using  System.ComponentModel;
None.gif
None.gif
namespace  CustomComponents
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 
InBlock.gif    
/// 地址集合
ExpandedSubBlockEnd.gif    
/// 

InBlock.gif    public class Address
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private String street = null;
InBlock.gif        
private String city = null;
InBlock.gif        
private String state = null;
InBlock.gif        
private String zip = null;
InBlock.gif
InBlock.gif        
public String Street
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return street;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                street 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public String City
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return city;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                city 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public String State
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return state;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                state 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public String Zip
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return zip;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                zip 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}



1.3 呈现控件

ContractedBlock.gif ExpandedBlockStart.gif
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtNone.gifusing System;
None.gif
using System.ComponentModel;
None.gif
using System.Web;
None.gif
using System.Web.UI;
None.gif
None.gif
namespace CustomComponents
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class Custom: Control
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private String name = null;
InBlock.gif        Address address 
= new Address();
InBlock.gif        
private Metier metier;
InBlock.gif        
private int age = 0;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性#region 属性
InBlock.gif        [Description(
"年龄")]
InBlock.gif        
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return age;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                age 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Description(
"姓名")]
InBlock.gif        
public String Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return name;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                name 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Description(
"职业")]
InBlock.gif        
public Metier CustomMetier
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return metier;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                metier 
= value;
ExpandedSubBlockEnd.gif            }<

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-402613/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12639172/viewspace-402613/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值