探知,不断发现
探知不断发现

项目中。我们经常需要一个当鼠标移过去就可以变换图像,离开又恢复到原来图像的控件,
也许大家都会说easy 但是这个写多起来也是比较郁闷。所以我们还是自己来写一个控件

  internal sealed class WebCategoryAttribute : System.ComponentModel.CategoryAttribute
    
{

        
internal WebCategoryAttribute(string category)
            : 
base(category)
        
{
        }


      

    }
 // class WebCategoryAttribute
    /// <summary>
    
/// Summary description for MyImageButton
    
/// </summary>

    public class MyImageButton : ImageButton
    
{
        [Editor(
"System.Web.UI.Design.ImageUrlEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"typeof(System.Drawing.Design.UITypeEditor))]
        [DefaultValue(
"")]
        [WebCategoryAttribute(
"Appearance")]
        [Bindable(
true)]
        
public string HoverImageUrl
        
{
            
get
            
{
                
string text = (string)this.ViewState["HoverImageUrl"];
                
if (text != null)
                
{
                    
return text;
                }

                
return string.Empty;

            }

            
set
            
{
                
this.ViewState["HoverImageUrl"= value;
            }

        }

        
protected override void AddAttributesToRender(HtmlTextWriter writer)
        
{
            
if (HoverImageUrl.Length != 0)
            
{
                
base.Attributes.Add("onmouseover"string.Format("this.src=\"{0}\""base.ResolveUrl(HoverImageUrl)));
                
base.Attributes.Add("onmouseout"string.Format("this.src=\"{0}\""base.ResolveUrl(ImageUrl)));
            }

            
base.AddAttributesToRender(writer);
        }

        
public MyImageButton()
        
{

        }

    }

}

很简单就可以搞定了。 比较复杂的地方是。HoverImageUrl 你必须为它指定一个Editor
posted on 2007-09-18 17:15  lovebanyi  阅读(313)  评论(0编辑  收藏  举报