Thursday, September 25, 2008

Some good practices of creating custom .net user control

1. No data binding or logic should be contained in the code.

2. There should be some property to data bind or access the control.

3. JavaScript should be place inside in a .js file because it makes it very easy to make global JavaScript changes to the application.

4. As a web page may contain several instance of the custom control, the JavaScript should not be included for each instance. So for including the JavaScript just for once per page the following code can written in the Page_Load(object sender, EventArgs e) method:

if(!this.Page.ClientScript.IsClientScriptBlockRegistered(keyClientBlock))
{
this.Page.ClientScript.
RegisterClientScriptBlock(this.Page.GetType(),
"keyClientBlock",jScript);
}
Here jScript string contains the JavaScript code.

5. If you need to execute some this javascript to initialize some tool then use RegisterStartupScript() method. The parameter list of this method is same as RegisterClientScriptBlock(). The difference between RegisterStartupScript() & RegisterClientScriptBlock() is that the first one puts the script at the bottom of the ASP.NET page and the latter puts that at the top of the page.

2 comments:

Facebook Pretender said...

Very good advice..
I have got some problems when I used data bindings in the custom control code...

From the very next time I will follow this..


And hope will post something about the custom control on desktop..

Sadique said...

Please send your blog url when you will post a blog on custom control in desktop application