自定义组件

通过继承WebControl和IPostBackEventHandler,封装一个自定义的按钮。

/*

<!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;

namespace WebControls
{
    
/// <summary>
    
/// 按钮
    
/// </summary>
    [DefaultProperty("Text")]
    
public class CButton : WebControl, IPostBackEventHandler
    {
        
private static readonly object ClickEvent = new object();

        
/// <summary>
        
/// 单击事件
        
/// </summary>
        public event EventHandler Click
        {
            add
            {
                Events.AddHandler(ClickEvent, value);
            }
            remove
            {
                Events.AddHandler(ClickEvent, value);
            }
        }

        
/// <summary>
        
/// 定义
        
/// </summary>
        
/// <param name="e"></param>
        protected virtual void OnClick(EventArgs e)
        {
            EventHandler clickEvent 
= Events[ClickEvent] as EventHandler;
            
if (clickEvent != null)
            {
                clickEvent(
this, e);
            }
        }

        
/// <summary>
        
/// 执行事件
        
/// </summary>
        
/// <param name="eventArgument"></param>
        public void RaisePostBackEvent(string eventArgument)
        {
            OnClick(
new EventArgs());
        }

        
/*// Defines the Click event.
        public event EventHandler Click;

        //Invoke delegates registered with the Click event.
        protected virtual void OnClick(EventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }


        // Define the method of IPostBackEventHandler that raises change events.
        public void RaisePostBackEvent(string eventArgument)
        {
            OnClick(new EventArgs());
        }
*/

        
/// <summary>
        
/// Text属性
        
/// </summary>
        [Description("Text"), Bindable(false), Category("Appearance"), DefaultValue("Button")]
        
public string Text
        {
            
get { return ViewState[this.UniqueID + "_Text"].ToString(); }
            
set { ViewState[this.UniqueID + "_Text"= value; }
        }

        
/// <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)
        {
            
if (Page != null)
            {
                Page.VerifyRenderingInServerForm(
this);  //确保此控件在form表单内
            }
            EventHandler clickEvent 
= (EventHandler)Events[ClickEvent];
            
if (clickEvent != null)
            {
                
string js = "";
                
if (this.Attributes["OnClick"!= null)
                {
                    js 
= this.Attributes["OnClick"].ToString();
                    
if (!js.EndsWith(";"))
                        js 
+= ";";
                }
                
this.Attributes.Add("OnClick", js + Page.ClientScript.GetPostBackEventReference(thisnulltrue));
            }
            
base.Render(writer);
            
//writer.Write(string.Format("<button type=\"submit\" class=\"SIActBtn\"><span class=\"btn\"><span class=\"btn\">{0}</span></span></button>", Text));
        }
    }
}

测试页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="button.aspx.cs" Inherits="Web.button" %>
<%@ Register Assembly="WebControls" Namespace="WebControls" TagPrefix="ht" %>
<!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>Untitled Page</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(images/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(images/imgVIPrmryBtnLt.gif) no-repeat top left; }
     .SIActBtn .btn .btn 
{ background:url(images/btnViPrmry.gif) no-repeat right;background-position:100% 0; }
  
</style>
</head>
<body>
  
<form id="form" runat="server">
  
<asp:Literal ID="lt" runat="server" />
  
<ht:CButton ID="CButton" runat="server" Text="Next, Please Click Me!" onclick="CButton_Click"/><br />
  
<button class="SIActBtn"><span class="btn"><span class="btn">Next, Please Confirm Your Cart</span></span></button><br />

  
<ht:MyButton ID="MyButton" runat="server" onclick="CButton_Click"/><br />
  
</form>
</body>
</html>

也可以如下方式加载样式
/// <summary>
/// 加载样式和JavaScript
/// </summary>
/// <param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
    
string strCss = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "WebControls.style.css");
    
string strCssLink = string.Format("<link type=\"text/css\" rel=\"stylesheet\" href=\"{0}\"/>", strCss);
    Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "Css", strCssLink);
    
base.OnPreRender(e);
}
posted @ 2008-06-20 20:50  angushine  阅读(301)  评论(0编辑  收藏  举报