Reusing and Creating Server Controls
page 3 of 4
by Justin Lovell
Feedback
Average Rating: 
Views (Total / Last 10 Days): 23190/ 56

Reuse of Other Controls
It is a lot easier to inherit a control and do some modifications to it. The deciding factor of whether or not to take this route, is if a control already exists for the current use. If a control does exist, and it is not sealed (also known as, not inheritable; which should be the case) then go ahead – do your tweaking... why re-invent the wheel?

On the previous page, I mentioned that a control developer my want to change the tag that encloses the text to the <div> tag. The two examples below (respectively) do the exact same thing, render a <div> as the HTML which will contain the output. This one inherits the CustomLabel control (which was done on the previous page that inherited from the WebControl class):

// [C#]
public class DivLabelControl : CustomLabel {
protected override HtmlTextWriterTag TagKey {
get { return HtmlTextWriterTag.Div; }
}
}
' [VB.NET]
Public Class DivLabelControl
Inherits CustomLabel
   Protected Overrides ReadOnly Property TagKey() As HtmlTextWriterTag
Get
Return HtmlTextWriterTag.Div
End Get
End Property
End Class

The following does the same... the only difference is that it inherits from the built-in Label control.

// [C#]
public class DivLabelControl : Label {
protected override HtmlTextWriterTag TagKey {
get { return HtmlTextWriterTag.Div; }
}
}
' [VB.NET]
Public Class DivLabelControl
Inherits Label
   Protected Overrides ReadOnly Property TagKey() As HtmlTextWriterTag
Get
Return HtmlTextWriterTag.Div
End Get
End Property
End Class

There was two objectives to show you both examples:

  • Show you that re-using controls is a ton of times easier.

  • Demonstrate with the logic in “Creating Controls from Scratch” which I stated it is a lot easier to extend the one that inherited from WebControl class.

But sometimes, re-using controls is not to extend or modify functionality/behaviour. One other common purpose that I have seen controls being re-used is to stop repetitive data-binding. An example is to have a list of customers on certain pages with a DataGrid control or DropDownList control. Fair enough with me – it could be placed in an user control but why should it be restricted to the user controls. In my experience with other developer's problems, when they re-use controls for common data binding, it is best to understand and learn the page life cycle like the back of your hand. I did discuss the page life cycle in the previous article over here. And if you followed the Guidelines and Trends for Developing Controls, solving the small issues that may occur will be relatively simple.

The tip to all the developers is this – override the OnInit method, do your data binding and then call the base's OnInit method. So your code for data binding should generally, look like this:


// [C#]
protected override void OnInit(EventArgs e) {
object dataSource; /* this variable can be anything that
the control that you are overriding can be bounded to */
   // give dataSource a value by getting the data
   DataSource = dataSource;
DataBind();
base.OnInit(e);
}
' [VB.NET]
Protected Overrides Sub OnInit(e As EventArgs)
Dim datSource As Object ' this variable can be anything that
' the control that you are overriding can be bounded to
   ' give dataSource a value by getting the data
   DataSource = datSource
DataBind()
MyBase.OnInit(e)
End Sub

View Entire Article

User Comments

Title: AddAttribute/RenderBeginTag sequence   
Name: Kevmeister
Date: 2005-12-08 8:50:14 PM
Comment:
Shouldn't the AddAttribute calls be executed *before* the RenderBeginTag?
Title: Sockets   
Name: Madhusudan
Date: 2004-09-08 8:37:47 AM
Comment:
Sir,
I need to distribute a control that enacpsulates the socket class. Is it possible to inherit from the socket class and write a custom web control ?
Thanks
Title: Re: composite control   
Name: Justin Lovell
Date: 2004-08-24 1:23:42 PM
Comment:
Senthil,

I have already written an article on the basics of databinding for server controls. You can read this information from here:

http://aspalliance.com/391

And if you want to see how the templates part work out, then you can also read another article that I have written over here:

http://aspalliance.com/366
Title: composite control   
Name: Senthil
Date: 2004-08-24 9:16:32 AM
Comment:
sir
I need to create a user control i want to assign various properties to it ,all the text propeties etc are felt easy to me,but i need to bind the database to my serve control and retrive data to that as like how the boundcolumn hav dataTextField like that i want to create and bound my data to the control.im struggling to it for the past wk's can u help me regd this

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 9:30:55 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search