Custom Search

March 3, 2008

User Controls for Reusable Forms

ASP.NET Web User Controls are very useful to minimize the development time of a project. It helps to reuse the codes all over again without recoding it. Image if you created a code that has 1000 lines and used in two tables. This would take you to change about 50%-80% of your codes in the second table. However, if you used Web User Controls, you will only need to change/set the property you want and takes you more or less 10% total time to put it in the second table. Another advantage of User Controls is that if you change anything in the code, you don't need to change all the other similar codes, all the others will automatically changed.

STANDARDIZATION NOTES:
  • You must use User Controls if that code can still be used in other forms.
  • If the query varies but the results are still the same usage of User Control is Recommended.
Example:
Create a blogging table. Blogs will be differentiated as Draft and Published. All Unregistered Users can only view all the blogs published by all Registered Users. Registered user can still view all published blogs by all users but has a management page where he/she can view all his blog work may it published or still a draft. Draft Blogs are in one table and Published Blogs are on the other table.

In this example there are three queries, the All Published (AP), Published by User (PU), and Drafted by User (DU). There is also one table, the blogging table (BT) but will be use for three times, in the Blog page, Management Page Draft area, and Management Page Published area. To save development time it is obvious to use the ASP.NET Web User Control. However there three different queries that we must follow. To solve this we must have a User Control Property that determines what query to be used. You can create an Enumeration to be represent those queries.

public enum QueryType{ AllPublished, Drafted, Published, None } //None Represents nothing was selected and returns nothing.

To set the Property:

public QueryType ViewBlog{
set{

if (value == QueryType.AllPublished){
//Do the query here
}

else if (value == QueryType.Published){
//Do the query here
}

else if (value == QueryType.Drafted){
//Do the query here
}

}
}

If you add this User Control in the web page and look at its property, you will see another property (ViewBlog) in the MISC property. Use this property to set the values of the User Control.