• ASP.NET 2.0中的友好CSS控件适配器的应用 用ul、li呈现CheckBoxList和RadioButtonList


    [源码下载]


    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
    {
        
    protected 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>

    都有什么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>
            
    &nbsp;</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>
            官网:
    <href="http://www.asp.net/cssadapters/" target="_blank">http://www.asp.net/cssadapters/</a>
        
    </p>
    </asp:Content>

    运行结果
    CheckBoxList和RadioButtonList将以ul、li呈现


    OK
    [源码下载]
  • 相关阅读:
    7-20 (样卷)统计单词的个数 (40 分)
    7-21 删除字符 (30 分)
    7-19 计算有n个字符串中最长的字符串长度 (40 分)
    7-16 列表数字元素加权和(1) (40 分)
    7-17 列表元素个数的加权和(1) (40 分)
    7-15 求出歌手的得分 (40 分)
    7-10 jmu-python-异常-学生成绩处理基本版 (15 分)
    7-11 jmu-python-分段函数&数学函数 (15 分)
    7-12 产生每位数字相同的n位数 (30 分)
    7-9 jmu-python-异常-学生成绩处理专业版 (25 分)
  • 原文地址:https://www.cnblogs.com/webabcd/p/838421.html
Copyright © 2020-2023  润新知