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()
{

base.CreateChildControls();

_txtBox = new TextBox();
this.Controls.Add(_txtBox);

_txtBox.Text = _defaultText;

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

_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):

Page.RegisterRequiresPostBack(CustomWebCtl1);
Page.RegisterRequiresRaiseEvent(CustomWebCtl1);

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;
}
else
{
return false;
}

}

public virtual void RaisePostDataChangedEvent()
{

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

}

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)
PostBacks
-----------------------------------------------------------------------------------------
LoadPostData ---> Page_Load (check for wired events ---> RaisePostDataChangedEvent

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

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

Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm