今天你写控件了吗?----ASP.net控件开发系列(八)

怎样更改默认的控件分析逻辑
 ------用PersistChildren(false)和ControlBuilder来定制ASP.net对控件标签对中的内容的分析

“我的地盘,我做主”


 写到这里,我把foobar播放的音乐换成了周杰伦的歌,虽然不是“我的地盘”。
 我们来回顾一下第三篇中的一段代码:

None.gif < asp:DropDownList  id ="DropDownList1"  runat ="server"  Font-Bold ="True" >
None.gif 
< asp:ListItem  Value ="1" > 1 </ asp:ListItem >
None.gif 
< asp:ListItem  Value ="2" > 2 </ asp:ListItem >
None.gif
</ asp:DropDownList >
None.gif

 我不知大家有没有注意到一个现象,<asp:ListItem>内含的内容不能有页面对象,那么如果有了分析器如何来处理呢?。这就透露了一个信息给我们,对于页面元素的分析处理,我们是插得上手的!(噢,比动感地带还好。)
 那么我们怎么来干预控件元素的分析呢?
 1、ParseChildrenAttribute
 2、ControlBuilderAttribute
 ParseChildren用来指定控件的分析逻辑,它有bool一个参数,true表示控件标签中的内容解释为属性,解析器用一组内定的控件生器来解析嵌套的属性、子属性、模板、集合属性。你还可以用ParseChildren(true,"PropertyName")来指定嵌套内容传入哪个属性,WebControl内已经声明为true。那么如果为false,怎么分析呢?在这种情况下,解析器用与控件相关的ControlBuilder来解释控件开始和结标签的内容,为将里面的内容解释为对象,文本也会解释为LiteralControls,然后通过控件的AddParsedSubObject方法添加到控件中Controls中。
 好,回过头来看ListItem,ListItem不是从WebControl继承而来,同时,它也没有声明为ParseChildren(true),那么它是怎么实现自己的分析逻辑的呢?答案就是它使用了ControlBuilderAttribute

None.gif [ControlBuilder( typeof (ListItemControlBuilder)), dot.gifdot.gif]
None.gif
public   sealed   class  ListItem : IStateManager, IParserAccessor, IAttributeAccessor
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif dot.gifdot.gif
ExpandedBlockEnd.gif}

None.gif

再来看ListItemControlBuilder类

None.gif public   class  ListItemControlBuilder : ControlBuilder
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
public ListItemControlBuilder()
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
ExpandedSubBlockEnd.gif }

InBlock.gif 
public override bool AllowWhitespaceLiterals()
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif        
return false;//除去控件标签对嵌套内容首尾空白
ExpandedSubBlockEnd.gif
 }

InBlock.gif 
InBlock.gif 
public override bool HtmlDecodeLiterals()
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif        
return true;//删除HTML编码
ExpandedSubBlockEnd.gif
 }

InBlock.gif 
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif

这里,我们可以不用ControlBuilder来使标签中禁用HTML编码,我们可以重载一个从WebControl继承下的控件的AddParsedSubObject方法

 

ExpandedBlockStart.gif ContractedBlock.gif          protected   override   void  AddParsedSubObject( object  obj)  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (obj is LiteralControl) dot.gif{
InBlock.gif                Text 
= ((LiteralControl)obj).Text;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
else dot.gif{
InBlock.gif                
throw new ArgumentException(
InBlock.gif                    
"The inner content must contain static text ");
ExpandedSubBlockEnd.gif            }

InBlock.gif  
// do anything you wantdot.gifdot.gif.
ExpandedBlockEnd.gif
        }

None.gif
None.gif

请注意,这种做法,效率是不如ControlBuilder方法的,因为它会在每次请求实现逻辑时都执行一次,而前者只在代码产生前的解析期被执行一次。
另外,比WebControl继承下来的控件,要实现用ControlBuilder控制生成内容对象,要记得声明ParseChilder(false)。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值