惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
MyScale Blog
MyScale Blog
D
Docker
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
Recent Announcements
Recent Announcements
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
博客园_首页
S
SegmentFault 最新的问题
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
Stack Overflow Blog
Stack Overflow Blog
G
Google Developers Blog
F
Fortinet All Blogs
A
About on SuperTechFans
博客园 - Franky
腾讯CDC
T
Tailwind CSS Blog
量子位
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Register - Security
The Register - Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
宝玉的分享
宝玉的分享
C
Cyber Attacks, Cyber Crime and Cyber Security
H
Help Net Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
SecWiki News
SecWiki News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
F
Full Disclosure
Y
Y Combinator Blog
S
Secure Thoughts
Webroot Blog
Webroot Blog
C
Check Point Blog
C
CERT Recently Published Vulnerability Notes
I
Intezer
Know Your Adversary
Know Your Adversary
Scott Helme
Scott Helme

博客园 - 心利

DocOptimizer 0.9.0 Beta Released SharePoint GroupedItemPicker Control Drag & Drop between SharePoint Document libraries Improving SharePoint User Experience With JQuery-Client Side Form Validate SharePoint How To:定位错误:“An SPRequest object was not disposed .." SharePoint Tips and Tricks --如何用JS向PeopleEditor填充数据 SharePoint Tips and Tricks -- Ribbon,People Editor 使用T4为数据库自动生成实体类(C#) (Tips&Tricks)用客户端模板精简JavaScript代码 (Tips &Tricks)如何为Windows Mobile 创建拨号连接--C# System.Xml FAQ Part 1 Windows Ce vs Windows Mobile Asp.net程序中生成Excel报表 My Page StartKit项目概览 做一个更好的程序员 .Net类库中实现的HashTable 为Asp.net控件写单元测试(ViewState) ViewState 简述一(With Example And Apply to Asp.net) 桌面背景:rss新闻阅读器(图)
使用Control Adapters优化Asp.net控件
心利 · 2007-06-01 · via 博客园 - 心利
 

有些时候Asp.net 控件默认状态下生成的html代码,不能满足一些特定的需要。比如

我们想让用户做一些选择,可以很容易的用如下代码实现

<asp:CheckBoxList runat="server">

   <asp:ListItem Text="One" />

   <asp:ListItem Text="Two" />

   <asp:ListItem Text="Three" />

</asp:CheckBoxList>


默认状态下CheckBoxList会将这些选项放在一个table标签里,但是也许有个别情况不适合使用table,而需要一个un-ordered list(ul)。当然我们可以重新写一个继承于CheckBoxList的控件,但是使用Control Adpater会更容易,并且还有一些额外的好处。

首先看一下实现:

1 写一个继承自WebControlAdapter的类,如下

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.Adapters;

public class RadioButtonListAdapter : WebControlAdapter

{

   protected override void Render(HtmlTextWriter writer)

   {

       ListControl targetControl = this.Control as ListControl;

       // If the control that this adapter is pointing to is not

       // a ListControl (RadioButtonList or CheckBoxList) then

       // we don't want to change the rendering.

       if (targetControl == null || targetControl is IRepeatInfoUser == false)

       {

base.Render(writer);

           return;

       }

       writer.WriteBeginTag("ul");

       if (targetControl.CssClass.Length > 0)

       {

         writer.WriteAttribute("class", targetControl.CssClass);

       }

       writer.Write(">");

       IRepeatInfoUser repeaterInfo = (IRepeatInfoUser)this.Control;

       for (int i = 0; i < targetControl.Items.Count; i++)

       {

           writer.WriteFullBeginTag("li");

           repeaterInfo.RenderItem(ListItemType.Item, i, new RepeatInfo(), writer);

           writer.WriteEndTag("li");

       }

       writer.WriteEndTag("ul");

   }

}


2,新建一个asp.net 文件夹 App_Browsers,在其中添加一个.browser文件,添加如下内容

<browsers>

<browser refID="Default">

       <controlAdapters>

           <adapter controlType="System.Web.UI.WebControls.CheckBoxList"

              adapterType="RadioButtonListAdapter" />

           <adapter controlType="System.Web.UI.WebControls.RadioButtonList"

              adapterType="RadioButtonListAdapter" />

       </controlAdapters>

   </browser>

</browsers>


好了,一切ok。注意到了没有?我们并没有改变先前的asp.net代码.这就是个非常重要的好处啊。

Ps : Most of information and code of this article come from Timothy Khouri's

Cool website SingingEels