【Clingingboy】asp.net控件开发基础:接触自定义控件

asp.net本身提供了很多控件,提供给我们这些比较懒惰的人使用,我认为控件的作用就在此,因为我们不想重复工作,所以要创建它,这个本身便是一个需求的关系,所以学习控件开发很有意思.

wrox网站上有本书 Professional ASP.NET 2.0 Server Control and Component Development

现在还没有出版,但网站上放出了代码,所以正好下载过来学习一下.

我看过前几章代码,环环相扣,作者用不同的知识向我们展示同一个效果,所以循序渐进的学下来很有好处.

虽然自己对控件开发还不是很熟悉,但我感觉以下几点很重要,是我自己总结的

1.了解控件之间的继承关系

  最好是先看看看System.Web.UI命名空间

(1)Control 类,所有的控件都共享的一个类,你需要去看下其里面受保护的几个方法和属性,虽然一下看不完,以后会发现常常用到这些方法

大家可以在MSDN看一下其派生类

(2)HtmlTextWriter 类

不得不了解的一个类,主要工作就是我们写的标记字符和文本输出

2.重写方法

(1) 必须继承Control类
(2) 重写Control类的Render方法,这个是必须的,因为其他控件都继承了Control 类类,所以几乎所有控件都有这个方法

3.熟悉元数据

大家都知道asp.net控件属性在编辑器上是分类的,如外观,行为,布局等,每个属性还给出了解释

简单的元数据就是起到这个作用,当然你也可以不加,但使用了元数据让人感到有亲切感,写法如

[CategoryAttribute("Appearance")]

要使用元数据,必须引用System.ComponentModel命名控件,一般你如果写组件的话,不可能不用到这样类库。具体的MSDN上有所介绍。

一.输出字符串
说多了没意思,还是来演练吧。首先你得了解HTML。来看下面代码,效果就是输出HTML到客户端

示例一

ContractedBlock.gif ExpandedBlockStart.gif
<!--

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

--&gtNone.gifusing System;
None.gif
using System.Web.UI;
None.gif
None.gif
namespace CustomComponents
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 
InBlock.gif  
/// Summary description for CreditCardForm
ExpandedSubBlockEnd.gif  
/// 

InBlock.gif  public class CreditCardForm1 : Control
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
protected override void Render(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      writer.Write(
"
" );
InBlock.gif      writer.Write(
" ");
InBlock.gif      writer.Write(
"Payment Method");
InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"Visa");
InBlock.gif      writer.Write(
"MasterCard");
InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"
" );
InBlock.gif      writer.Write(
" ");
InBlock.gif      writer.Write(
"Credit Card No.");
InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"
" );
InBlock.gif      writer.Write(
" ");
InBlock.gif      writer.Write(
"Cardholder's Name");
InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"
" );
InBlock.gif      writer.Write(
" ");
InBlock.gif      writer.Write(
"Expiration Date");
InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"");
InBlock.gif      
for (int day = 1; day < 13; day++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
if (day < 10)
InBlock.gif          writer.Write(
"" + day.ToString() + "'>" + "0" + day.ToString() + "");
InBlock.gif        
else
InBlock.gif          writer.Write(
"" + day.ToString() + "'>" + day.ToString() + "");
ExpandedSubBlockEnd.gif      }

InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"&nbsp");
InBlock.gif      writer.Write(
"");
InBlock.gif      
for (int year = 2005; year < 2015; year++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        writer.Write(
"" + year.ToString() + "'>" + year.ToString() + "");
ExpandedSubBlockEnd.gif      }

InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"
" );
InBlock.gif      writer.Write(
" ");
InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"");
InBlock.gif      writer.Write(
"
" );
InBlock.gif      writer.Write(
" " );
InBlock.gif
InBlock.gif      
base .Render(writer);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif

 

效果很简单,其实就一直在输出HTML再加几个属性,大家可以直接把代码放在App_Code文件夹里,就可自动编译,当然也可以创建web控件库.
注意要继承Control类,重写Render方法,用HtmlTextWriter类的Write输出HTML

使用控件

(1).需要先注册一下

(2) 然后就使用标签输出效果

下为效果图


二.改善,加入属性和元数据

可能上面做出的 控件毫无用处,但却可以让你熟悉一下步骤,上面的控件定的很死,没有定义任何属性,用处不大,下面来改造

我们来定义常用属性,然后再输出,这样我们就可以修改控件的属性了,

示例二

ContractedBlock.gif ExpandedBlockStart.gif
<!--

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

--&gtNone.gifusing System;
None.gif
using System.Web.UI;
None.gif
using System.ComponentModel;
None.gif
None.gif
namespace CustomComponents
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  [DefaultPropertyAttribute(
"CardholderNameText")]
InBlock.gif    [ToolboxData(
@"InBlock.gif    PaymentMethodText='信用卡类型' CreditCardNoText='信用卡卡号' 
InBlock.gif    CardholderNameText='信用卡持有者姓名' SubmitButtonText = '提交'  
InBlock.gif    runat='server'>{0}:CreditCardForm2>
")
InBlock.gif    ]
InBlock.gif  
public class CreditCardForm2 : Control
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
private string paymentMethodText = "信用卡类型";
InBlock.gif    
private string creditCardNoText = "信用卡卡号";
InBlock.gif    
private string cardholderNameText = "信用卡持有者姓名";
InBlock.gif    
private string expirationDateText = "最后使用时间";
InBlock.gif    
private string submitButtonText = "提交";
InBlock.gif
InBlock.gif    [BrowsableAttribute(
true)]
InBlock.gif    [DescriptionAttribute(
"获取和设置信用卡类型")]
InBlock.gif      [DefaultValueAttribute(
"信用卡类型")]
InBlock.gif    [CategoryAttribute(
"Appearance")]
InBlock.gif    
public virtual string PaymentMethodText
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
get dot.gifreturn this.paymentMethodText; }
ExpandedSubBlockStart.gifContractedSubBlock.gif      
set dot.gifthis.paymentMethodText = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [BrowsableAttribute(
true)]
InBlock.gif    [DescriptionAttribute(
"获取或设置信用卡卡号")]
InBlock.gif    [DefaultValueAttribute(
"信用卡卡号")]
InBlock.gif    [CategoryAttribute(
"Appearance")]
InBlock.gif    
public virtual string CreditCardNoText
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
get dot.gifreturn this.creditCardNoText; }
ExpandedSubBlockStart.gifContractedSubBlock.gif      
set dot.gifthis.creditCardNoText = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [BrowsableAttribute(
true)]
InBlock.gif      [DescriptionAttribute(
"获取或设置信用卡持有者姓名")]
InBlock.gif    [DefaultValueAttribute(
"信用卡持有者姓名")]
InBlock.gif    [CategoryAttribute(
"Appearance")]
InBlock.gif    
public virtual string CardholderNameText
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
get dot.gifreturn this.cardholderNameText; }
ExpandedSubBlockStart.gifContractedSubBlock.gif      
set dot.gifthis.cardholderNameText = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [BrowsableAttribute(
true)]
InBlock.gif      [DescriptionAttribute(
"获取或设置最后使用时间")]
InBlock.gif      [DefaultValueAttribute(
"最后使用时间")]
InBlock.gif    [CategoryAttribute(
"Appearance")]
InBlock.gif    
public virtual string ExpirationDateText
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
get dot.gifreturn this.expirationDateText; }
ExpandedSubBlockStart.gifContractedSubBlock.gif      
set dot.gifthis.expirationDateText = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [BrowsableAttribute(
true)]
InBlock.gif    [DescriptionAttribute(
"获取或设置按钮标签")]
InBlock.gif    [DefaultValueAttribute(
"提交")]
InBlock.gif    [CategoryAttribute(
"Appearance")]
InBlock.gif    
public virtual string SubmitButtonText
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
get dot.gifreturn

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值