technofantasy

博客园 首页 新随笔 联系 订阅 管理
在项目中要用到可以切换状态的图片按钮(就是鼠标离开是一个样子,鼠标移上去是一个样子,鼠标离开是一个样子)
自己做了一个简单的web控件实现这样的效果:


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace BusinessObjects.WebControls
{
    
/// <summary>
    
/// Summary description for ImagedButton.
    
/// </summary>

    [DefaultProperty("Text"), 
        ToolboxData(
"<{0}:ImagedButton runat=server></{0}:ImagedButton>")]
    
public class ImagedButton : System.Web.UI.WebControls.ImageButton 
    
{
        
private string text;
        
private string enterimage;
        
private string clickimage;
        
private string clickaction;
    
        [Bindable(
true), 
            Category(
"Appearance"), 
            DefaultValue(
"")] 
        
public string Text 
        
{
            
get
            
{
                
return text;
            }


            
set
            
{
                text 
= value;
            }

        }


        [Category(
"Appearance"), DefaultValue(""), Description("鼠标点击时执行的脚本")]
        
public string onClick
        
{
            
get
            
{
                
return clickaction;
            }

            
set
            
{
                clickaction 
= value;
            }

        }


        [Category(
"Appearance"), DefaultValue(""), Description("鼠标进入时的图片"), EditorAttribute(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
        
public string EnterImage
        
{
            
get
            
{
                
return enterimage;
            }

            
set
            
{
                enterimage 
= value;
            }

        }


        [Category(
"Appearance"), DefaultValue(""), Description("鼠标按下时的图片"), EditorAttribute(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
        
public string ClickImage
        
{
            
get
            
{
                
return clickimage;
            }

            
set
            
{
                clickimage 
= value;
            }

        }

        
        
/// <summary> 
        
/// Render this control to the output parameter specified.
        
/// </summary>
        
/// <param name="output"> The HTML writer to write out to </param>

        protected override void Render(HtmlTextWriter output)
        
{
            
//output.Write(Text);
            string strClick = "";
            
if(onClick != null)
            
{
                strClick 
= "onclick='" + onClick + "'";
            }


            
string strStyle = " style=\"";
            foreach(string s in Style.Keys)
            
{
                strStyle 
+= s;
                strStyle 
+= ":";
                strStyle 
+= Style[s].ToString();
                strStyle 
+= ";";
            }

            strStyle 
+= "\"";

            output.Write(
"<img src = '" + this.ImageUrl + "" + strStyle + " onmouseenter='this.src=\"" + 
                    EnterImage + "\"' onmousedown='this.src=\"" + ClickImage + 
                    
"\"' onmouseup='this.src=\"" + ImageUrl + "\"' onmouseleave='this.src=\"" + ImageUrl + "\"'" + strClick + ">");
        }

    }

}


posted on 2006-02-17 16:57  陈锐  阅读(260)  评论(0)    收藏  举报