All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
使用Control,Adapters优化Asp.net控件_.net资料_编程技术

Writer: aaa Article type: Programming skills(编程技巧) Time: 2013/12/17 7:46:43 Browse times: 358 Comment times: 0

使用Control,Adapters优化Asp.net控件_.net资料_编程技术


Head photo

Go homepage
Upload pictures
Write articles

使用Control Adapters优化Asp.net控件_.net资料_编程技术-你的首页-uuhomepage.com

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

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





默认状态下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文件,添加如下内容




adapterType="RadioButtonListAdapter" />
adapterType="RadioButtonListAdapter" />


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






There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.