组件开发中常用的属性

来源:互联网 发布:淘宝宝贝评价多久清空 编辑:程序博客网 时间:2024/05/23 12:20

DefaultEvent( "ClickNext" ):指定组件的默认事件
DefaultProperty( "NextText" ):指定组件的默认属性
Bindable(true or false):指定属性是否通常用于绑定
Category( "Appearance" ):指定其属性或事件将显示在可视化设计器中的类别
DefaultValue( typeof( Color ) , "" ):指定属性的默认值
Description( "The background color"):指定属性或事件的说明
TypeConverter(typeof( WebColorConverter )):指定用作此特性所绑定到的对象的转换器的类型
Browsable(true or false):指定一个属性或事件是否应显示在“属性”窗口中
DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden ):指定在设计时序列化组件上的属性时所使用的持久性类型

==========================================================================

简单控件 事件


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


namespace MyControls
{
    
/// <summary>
    
/// Summary description for MyEventManager.
    
/// </summary>

    [DefaultEvent("NextClick"),DefaultProperty("text")]
    
public class MyEventManager : System.Web.UI.WebControls.WebControl,IPostBackEventHandler
    
{
        
        
public event EventHandler NextClick;
        
public event EventHandler PreClick;

        [Bindable(
true),Category("Behavior"),DefaultValue(""),Description("text ")]
        
public string text
        
{
            
get
            
{
                
return ( (string)ViewState["text"== null )? string.Empty:(string)ViewState["text"] ;
            }

            
set
            
{
                ViewState[
"text"= value;
            }

        }


        
protected virtual void OnNextClick(EventArgs e)
        
{
            
if(NextClick != null)
            
{
                NextClick(
this,EventArgs.Empty);
            }


        }


        
protected virtual void OnPreClick(EventArgs e)
        
{
            
if(PreClick != null)
            
{
                PreClick(
this,EventArgs.Empty);
            }


        }


        
void IPostBackEventHandler.RaisePostBackEvent(string EventArguments)
        
{
            
if( EventArguments =="Pre" )
            
{
                OnPreClick(EventArgs.Empty);
                Page.Trace.Warn(
"Pre Button Click");
            }

            
else
            
{
                OnNextClick(EventArgs.Empty);
                Page.Trace.Warn(
"Next Button Click");
            }

        }


        [Bindable(
true), 
            Category(
"Appearance"), 
            DefaultValue(
"")] 
        
        
        
protected override void Render(HtmlTextWriter output)
        
{
            
//output.Write(Text);
            this.Attributes.AddAttributes(output);

            output.AddAttribute(HtmlTextWriterAttribute.Onclick,Page.GetPostBackEventReference(
this,"Pre"));
            output.AddAttribute(
"language","javascript");

            output.RenderBeginTag(HtmlTextWriterTag.Button);
            output.Write(
"Pre");
            output.RenderEndTag();

            output.AddAttribute(HtmlTextWriterAttribute.Onclick,Page.GetPostBackEventReference(
this,"Next"));
            output.AddAttribute(
"language","javascript");

            output.RenderBeginTag(HtmlTextWriterTag.Button);
            output.Write(
"Next");
            output.RenderEndTag();


            
base.Render(output);
        }

    }

}