ASP.NET 2.0中的友好CSS控件适配器的应用 - 用ul、li呈现CheckBoxList和RadioButtonList
作者:webabcd
介绍
控件适配器(Control Adapters)可以让你改变通过ASP.NET控件生成的HTML标记。http://www.asp.net/cssadapters/有详细的介绍和示例,但是没有用ul、li呈现CheckBoxList和RadioButtonList的例子(可能是因为太简单了,不屑去写),那就让我们来实现它吧,顺便熟悉一下如何开发控件适配器。
示例
App_Code/ControlAdaptersSample.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.Adapters;
/**//// <summary>
/// ControlAdaptersSample 的摘要说明
/// </summary>
public class ControlAdaptersSample : WebControlAdapter
{
override void Render() override void Render(HtmlTextWriter writer)
{
ListControl listControl = this.Control as ListControl;
IRepeatInfoUser repeaterInfo = this.Control as IRepeatInfoUser;
if (listControl == null || repeaterInfo == null)
{
base.Render(writer);
return;
}
if (listControl.CssClass.Length > 0)
{
writer.AddAttribute("class", listControl.CssClass);
}
writer.RenderBeginTag(HtmlTextWriterTag.Ul);
for (int i = 0; i < listControl.Items.Count; i++)
{
writer.RenderBeginTag(HtmlTextWriterTag.Li);
repeaterInfo.RenderItem(ListItemType.Item, i, new RepeatInfo(), writer);
writer.RenderEndTag();
}
writer.RenderEndTag();
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.Adapters;
/**//// <summary>
/// ControlAdaptersSample 的摘要说明
/// </summary>
public class ControlAdaptersSample : WebControlAdapter
{
override void Render() override void Render(HtmlTextWriter writer)
{
ListControl listControl = this.Control as ListControl;
IRepeatInfoUser repeaterInfo = this.Control as IRepeatInfoUser;
if (listControl == null || repeaterInfo == null)
{
base.Render(writer);
return;
}
if (listControl.CssClass.Length > 0)
{
writer.AddAttribute("class", listControl.CssClass);
}
writer.RenderBeginTag(HtmlTextWriterTag.Ul);
for (int i = 0; i < listControl.Items.Count; i++)
{
writer.RenderBeginTag(HtmlTextWriterTag.Li);
repeaterInfo.RenderItem(ListItemType.Item, i, new RepeatInfo(), writer);
writer.RenderEndTag();
}
writer.RenderEndTag();
}
}
App_Browsers/ControlAdaptersSample.browser
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.CheckBoxList"
adapterType="ControlAdaptersSample" />
<adapter controlType="System.Web.UI.WebControls.RadioButtonList"
adapterType="ControlAdaptersSample" />
</controlAdapters>
</browser>
</browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.CheckBoxList"
adapterType="ControlAdaptersSample" />
<adapter controlType="System.Web.UI.WebControls.RadioButtonList"
adapterType="ControlAdaptersSample" />
</controlAdapters>
</browser>
</browsers>
都有什么browsers可用呢?可以在如下的路径中找到
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers
ControlAdaptersSample.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ControlAdaptersSample.aspx.cs"
Inherits="Control_ControlAdaptersSample" Title="友好CSS控件适配器的简单示例" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cph" runat="Server">
<style>
.ListControlCSS {}{ list-style:none; margin:0px; width: 400px; background-color: #CCC; }
.ListControlCSS INPUT {}{ background-color: #CCC; }
.ListControlCSS li{}{ float:left; width:100px; line-height:30px;}
</style>
<asp:CheckBoxList ID="cbl" runat="server" CssClass="ListControlCSS">
<asp:ListItem Text="One" />
<asp:ListItem Text="Two" />
<asp:ListItem Text="Three" />
<asp:ListItem Text="Four" />
<asp:ListItem Text="Five" />
<asp:ListItem Text="Six" />
<asp:ListItem Text="Seven" />
<asp:ListItem Text="Eight" />
<asp:ListItem Text="Nine" />
<asp:ListItem Text="Ten" />
</asp:CheckBoxList>
<div>
</div>
<asp:RadioButtonList ID="rbl" runat="server" CssClass="ListControlCSS">
<asp:ListItem Text="One" />
<asp:ListItem Text="Two" />
<asp:ListItem Text="Three" />
<asp:ListItem Text="Four" />
<asp:ListItem Text="Five" />
<asp:ListItem Text="Six" />
<asp:ListItem Text="Seven" />
<asp:ListItem Text="Eight" />
<asp:ListItem Text="Nine" />
<asp:ListItem Text="Ten" />
</asp:RadioButtonList>
<p>
官网:<a href="http://www.asp.net/cssadapters/" target="_blank">http://www.asp.net/cssadapters/</a>
</p>
</asp:Content>
Inherits="Control_ControlAdaptersSample" Title="友好CSS控件适配器的简单示例" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cph" runat="Server">
<style>
.ListControlCSS {}{ list-style:none; margin:0px; width: 400px; background-color: #CCC; }
.ListControlCSS INPUT {}{ background-color: #CCC; }
.ListControlCSS li{}{ float:left; width:100px; line-height:30px;}
</style>
<asp:CheckBoxList ID="cbl" runat="server" CssClass="ListControlCSS">
<asp:ListItem Text="One" />
<asp:ListItem Text="Two" />
<asp:ListItem Text="Three" />
<asp:ListItem Text="Four" />
<asp:ListItem Text="Five" />
<asp:ListItem Text="Six" />
<asp:ListItem Text="Seven" />
<asp:ListItem Text="Eight" />
<asp:ListItem Text="Nine" />
<asp:ListItem Text="Ten" />
</asp:CheckBoxList>
<div>
</div>
<asp:RadioButtonList ID="rbl" runat="server" CssClass="ListControlCSS">
<asp:ListItem Text="One" />
<asp:ListItem Text="Two" />
<asp:ListItem Text="Three" />
<asp:ListItem Text="Four" />
<asp:ListItem Text="Five" />
<asp:ListItem Text="Six" />
<asp:ListItem Text="Seven" />
<asp:ListItem Text="Eight" />
<asp:ListItem Text="Nine" />
<asp:ListItem Text="Ten" />
</asp:RadioButtonList>
<p>
官网:<a href="http://www.asp.net/cssadapters/" target="_blank">http://www.asp.net/cssadapters/</a>
</p>
</asp:Content>
本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/341975,如需转载请自行联系原作者