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