Custom Server Controls Vs Entirely Custom Controls

Sometimes you find yourself writing custom controls which uses server control provided by Microsoft.

These controls include TextBox, GridView and Button. With these control you don't have to implement IPostBackDataHandler (unless you're doing something customization) as you can wire the event shown here.

protected override void CreateChildControls()


_txtBox = new TextBox();

_txtBox.Text = _defaultText;

System.Web.UI.WebControls.Button _txtBtn = new Button();

_txtBtn.Click += new EventHandler(_txtBtn_Click);
_txtBtn.Text = _cmdText;


void _txtBtn_Click(object sender, EventArgs e)

////// triggerring take place here /////////


If you need to customized your control above to implement IPostBackDataHandler for your custom server control, then just register your component. Beware if you do that, your existing wired EVENT will no longer work.

//////////////////////// Fully Custom Server Control ////////////////////////

But when you're writing a server control whereby you have full control over the rendering of entire html codes, then you have to implement IPostBackDataHandler.

Just put these into your aspx file (registering the control):


In your custom server code you probably have such code implementation-

public virtual bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection values)

string _previousValue = defaultText;
string _ctlValue = values[postDataKey];

if (string.IsNullOrEmpty(_previousValue) || !_previousValue.Equals(_ctlValue))
this.defaultText = _ctlValue;
return true;
return false;


public virtual void RaisePostDataChangedEvent()

WebCtlEvent e = new WebCtlEvent();
e.Text = Page.Request.Form[this.ClientID + "_input"];


public virtual void OnSearching(WebCtlEvent e)
if (searching != null)
searching(this, e);


Just needed a place of put life cycle of a control for my own record......... :)

Stage 1
CreateChildControls -----> EnsureChildControls ----> Render ----> RenderControl

Stage 2 (Recylce itself depending on how many times user click on the page)
LoadPostData ---> Page_Load (check for wired events ---> RaisePostDataChangedEvent

---> RaisePostBackEvent ----> EnsureChildControls ----> force CreateChildControls

--->Render ----> RenderControls;


Popular posts from this blog

OpenCover code coverage for .Net Core

Android Programmatically apply style to your view

Using Custom DLL with IronPython / Scripts