自定义控件实现WebControl和Compositecontrol得区别

1. 当你的自定义控件实现WebControl或者Control类时候,如果你在重写 CreateChildControls方法来创建控件,那么你在重写Render方法来实现拖拽到页面控件自动生成,那么你必须在Render方法中在调用每个控件的RenderControl()方法之前调用 EnsureChildControls() 方法来确保控件不为NULL而导致的异常.
    public class MyControls : WebControl
    {
        
private Button bt;
        
protected override void CreateChildControls()
        {
            
base.CreateChildControls();
            Controls.Clear();
            bt 
= new Button();
            bt.ID 
= this.ClientID;
            bt.Text 
= "Submit";
            Controls.Add(bt);
        }
        
protected override void Render(HtmlTextWriter writer)
        {
            
base.Render(writer);
            EnsureChildControls();
            bt.RenderControl(writer);
        }
    }

如果你的自定义控件实现CompositeControl,因为在Render方法中如果判断在设计界面,那么编译器会强迫调用EnsureChildControls()
方法调用CreateChildControls()方法创建控件。所以你无需在Render方法中再次调用。

protected internal override void Render(HtmlTextWriter writer)
{
    
if (base.DesignMode)
    {
        
this.EnsureChildControls();
    }
    
base.Render(writer);
}

//完整实例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Specialized;
namespace MyCustomizeControl
{

    public class MyControl : CompositeControl,IPostBackDataHandler
    {
        
private static readonly object objLock = new object();
        
private static readonly object objLock2 = new object();

        private TextBox txb;
        
private Button bt;
        
private LinkButton lnk_Submit;

        private string _strText = string.Empty;
        
public string strUser
        {
            
get
            {
                EnsureChildControls();
                
return (ViewState["strUser"== null? string.Empty : ViewState["strUser"].ToString();
            }
            
set
            {
                EnsureChildControls();
                ViewState[
"strUser"= value;
            }
        }

        public event EventHandler MyClick
        {
            add {
                Events.AddHandler(objLock, value);
            }
            remove
            {
                Events.RemoveHandler(objLock, value);
            }
        }
        
public event EventHandler MyLinkClick
        {
            add
            {
                Events.AddHandler(objLock2, value);
            }
            remove
            {
                Events.RemoveHandler(objLock2, value);
            }
        }

        protected void OnHandler(EventArgs e)
        {
            EventHandler eventDelegate 
= Events[objLock] as EventHandler;
            
if (eventDelegate != null)
            {
                eventDelegate(
this, e);
            }
        }
        
protected void OnLinkHandler(EventArgs e)
        {
            EventHandler eventDelegate 
= Events[objLock2] as EventHandler;
            
if (eventDelegate != null)
            {
                eventDelegate(
this, e);
            }
        }

        public string SubmitName
        {
            
get {
                
return this.ClientID + "_Submit";
            }
        }
        
public string LinkButtonName
        {
            
get
            {
                
return this.ClientID + "_LinkButton";
            }
        }
        
public string TextBoxName
        {
            
get
            {
                
return this.ClientID + "_TxtName";
            }
        }

        protected override void CreateChildControls()
        {
           
//base.CreateChildControls();
            txb = new TextBox();
            txb.ID 
= TextBoxName;

            bt = new Button();
            bt.ID 
= SubmitName;
            bt.Text 
= "Submit";

            lnk_Submit = new LinkButton();
            lnk_Submit.Text 
= "MyLink";
            lnk_Submit.ID 
= LinkButtonName;

            Controls.Add(lnk_Submit);
            Controls.Add(txb);
            Controls.Add(bt);                  
        }

        private string _stringUserName = string.Empty;
        
public string UserName
        {
            
get {
                EnsureChildControls();
                
return (ViewState["UserName"== null? string.Empty : ViewState["UserName"].ToString();
            }
            
set {
                EnsureChildControls();
                ViewState[
"UserName"= value;
            }
        }
        
protected override void OnPreRender(EventArgs e)
        {
            
base.OnPreRender(e);
            
if (this.Page != null && this.Enabled)
            {
                
this.Page.RegisterRequiresPostBack(this);
            }
        }
        
protected override bool OnBubbleEvent(object source, EventArgs args)
        {
            
if (source.GetType() == typeof(System.Web.UI.WebControls.LinkButton))
            {
                OnLinkHandler(EventArgs.Empty);
            }
            
//if (source.GetType() == typeof(System.Web.UI.WebControls.Button))
           
// {
                OnHandler(EventArgs.Empty);
           
// }
            return true;//base.OnBubbleEvent(source, args);
        }

        #region IPostBackDataHandler Members

        public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            NameValueCollection nvc 
= postCollection;
            
string str = nvc[postDataKey + "$" + TextBoxName];
            
if (UserName != str)
            {
                UserName 
= str;
                
return true;
            }
            
else return true ;
          
//  throw new NotImplementedException();
        }

        public void RaisePostDataChangedEvent()
        {
            Context.Response.Write(
"hello");
           
// throw new NotImplementedException();
        }

        #endregion
    }

}

阅读更多
博主设置当前文章不允许评论。

博主推荐

换一批

没有更多推荐了,返回首页