|
Recent
Articles |
ASP.NET: Is It Too Easy? Some time ago, I wrote a post called "Is ASP.NET too difficult?" and I strongly believe that it is.
Yet at the same time it cause problems by making it too easy to do relative advanced programming. It doesn't seem...
ASP.NET: Make GridView Control Accessible The GridView is a new web control in ASP.NET 2.0 and is an improvement of the old DataGrid. One of the biggest issues with the DataGrid was the lack of standard compliance and accessibility. This has been fixed in the new GridView along with a lot of other things as...
ASP.NET - Block IP Addresses From Your Site Recently, one of my readers asked me how to block certain IP addresses from accessing his ASP.NET website. It was a good question that could be answered in multiple correct ways. My answer was a plug 'n...
ASP.NET: Maintain Scroll Position After Postbacks To maintain the scroll position after postbacks is important for larger web pages in order to let the user know exactly what is going on. It is good usability and something you would expect in modern web...
GZip Vs. Deflate - Compression And Performance After I wrote about a HTTP compression module in ASP.NET 2.0 one of my colleagues pointed out that the Deflate compression is faster than GZip.Because the HTTP compression module chooses GZip over Deflate if the browser allows it, I thought that I'd better...
App_Code: Why It's Better I remember when ASP.NET 2.0 beta 1 was released about 2 years ago and I eagerly tried it out. The first thing that struck me as strange was the new application folders like App_Code, App_Data, and App_Themes etc.
Refactoring Your ASP.NET Project One day at work i was refactoring my code as I do everyday, when it suddenly hit me that our ASP.NET projects were also refactored in a sense. For those of you who don't know what refactoring is, here's...
ASP.NET: Permanent Redirection The easiest way to make a redirection in ASP.NET is using Response.Redirect(url). What it actually does is, that it creates a response with the "302 (Object Moved)" status code and the target destination. It tells the...
|
|
|
02.27.07
Creating Reusable User Controls In ASP.NET
By
Mads Kristensen
In almost every web project of any size, you would probably use a lot of user controls to separate the content and UI logic.
Many of the user controls are used only at one place in the solution while others are used by various pages and other user controls.
The ones that are used throughout the website needs to be generic and loosely coupled from the context in which they are used. If not, they would contain a lot of code only to cater for the needs of certain pages or other controls in which they are embedded. This is bad.
The rules of object oriented programming apply to ASP.NET just as much as for code libraries, which include loose coupling, encapsulation, high cohesion and polymorphism. This is easily forgotten due to the apparent differences between classic programming and ASP.NET development.
Loose coupling
A user control should not contain any context specific code, but only do the one thing that it was designed to. If the control is called AddComments.ascx it should do nothing more than add comments. Look at the name and use it as a guide for what it should do, and equally as important, what not to do.
By making it loosely coupled you gain a lot of flexibility and reusability for the user control. You could even use it in other web projects because it is not dependant on the context in which it
lives.
Encapsulation
The inner workings of a user control should be kept invisible by the user of that control. That is does simply by making the various members private. The AddComments.ascx control only has to expose two properties and that is Name and Comment. Those properties should be made public and everything else should be kept private.
If the user control has some kind of events like a button click event, that button should not be public so the page can listen for the click event. Instead, you should create a public event that you raise internally from the buttons click event handler. By exposing the button you make it harder to understand and use the control. Here is how to add events.
High cohesion
Keep the control focused on the single task it was built to solve. All the methods should relate to each other in some way, so you don't end up having generic code in them.
In the AddComments.ascx control you might have some methods that clean the entered comments so it doesn't contain any JavaScript and other nasty things. You should consider moving those methods out of the user control, because they could represent a core functionality that isn't that much related to adding comments.
Continue reading this article.
About the Author: Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.
http://www.madskristensen.dk/
|