改进CButton

由于在上个版本中CButton无法支持验证控件,故此改变为继承Button,一切问题迎刃而解,代码如下

/*

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
  <title>Beads - UK wholesale beads,buttons and jewelry making supplies-eezyco.co.uk</title>
  <style type="text/css">
 button { padding:0;margin:0;border:0;background:none;text-indent:inherit; }
 * html button { width:1%;overflow:visible;margin:0 5px 0 0; }
 button .btn { background:url(
https://securepics.ebaystatic.com/aw/pics/buttons/btnViPrmry.gif);color:#fff;font-family:Arial;font-weight:bold;white-space:nowrap;cursor:pointer;display:block;height:24px;line-height:190%;padding:0 0 0 6px;vertical-align:middle; }
 button .btn .btn { padding:0 20px 0 14px; }
 .SIActBtn .btn { background:url(
https://securepics.ebaystatic.com/aw/pics/viewitem/imgVIPrmryBtnLt.gif) no-repeat top left; }
 .SIActBtn .btn .btn { background:url(
https://securepics.ebaystatic.com/aw/pics/buttons/btnViPrmry.gif) no-repeat right;background-position:100% 0; }
  </style>
</head>
<body>
<div>
 <button type="submit" tabindex="3" class="SIActBtn"><span class="btn"><span class="btn">Next, Please Confirm Your Cart</span></span></button>
</div>
</body>
</html>

*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.Design;
using System.Globalization;
using System.IO;

namespace WebControls
{
    
/// <summary>
    
/// 按钮
    
/// </summary>
    [DefaultProperty("Text")]
    
public class CButton : Button
    {
        
/// <summary>
        
/// 生成标记
        
/// </summary>
        protected override HtmlTextWriterTag TagKey
        {
            
get { return HtmlTextWriterTag.Button; }
        }

        
/// <summary>
        
/// 增加属性
        
/// </summary>
        
/// <param name="writer"></param>
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            
base.AddAttributesToRender(writer);
            writer.AddAttribute(
"class""SIActBtn");
        }

        
/// <summary>
        
/// 增加子控件
        
/// </summary>
        protected override void CreateChildControls()
        {
            
base.CreateChildControls();
            Literal lt 
= new Literal();
            lt.Text 
= string.Format("<span class=\"btn\"><span class=\"btn\">{0}</span></span>"this.Text);
            
this.Controls.Add(lt);
        }

        
/// <summary>
        
/// 生成Button
        
/// </summary>
        
/// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            StringWriter stringWriter 
= new StringWriter();
            HtmlTextWriter htmlWriter 
= new HtmlTextWriter(stringWriter);
            
base.Render(htmlWriter);
            
string html = stringWriter.ToString();
            
int index = html.ToLower().IndexOf("</button>");
            html 
= string.Format("{0}<span class=\"btn\"><span class=\"btn\">{1}</span></span></button>", html.Substring(0, index), this.Text);
            writer.Write(html);
        }
    }
}
posted @ 2008-06-21 11:16  angushine  阅读(378)  评论(0编辑  收藏  举报