Powered by Blogger.

Saturday, February 15, 2014

What are user controls and why they are very useful?




User controls enable you to group logically related content and controls together so they can be used as a single unit in content pages, master pages, and inside other user controls. A user control is actually a sort of mini-ASPX page in that it has a markup section and optionally a Code Behind file in which you can write code for the control.

To some extent, they look a bit like server controls in that they can contain programming logic and presentation that you can reuse in your pages.

User controls have the following similarities with normal ASPX pages:

1- They have a markup section where you can add standard markup and server controls.

2- They can be created and designed with Visual Web Developer in Markup, Design, and Split View.

3- They can contain programming logic, either inline or with a Code Behind file.

User controls have an .ascx extension instead of the regular .aspx extension.

User controls are added to the site like any other content type: through the Add New Item dialog box. Similar to pages, you get the option to choose the programming language and whether you want to place the code in a separate Code Behind file.

Although using controls for repeating content is already quite useful, they become even more useful when you add custom logic to them. By adding public properties or methods to a user control, you can influence its behavior at runtime. When you add a property to a user control, it becomes available automatically in IntelliSense and in the Properties Grid for the control in the page you’re working with, making it easy to change the behavior from an external file like a page.

User controls can greatly improve the maintainability of your site. Instead of repeating the same markup and code on many different pages in your site, you encapsulate the code in a single control,which can then be used in different areas of your site.

Some tips on User Controls:
Don’t overuse user controls. User controls are great for encapsulating repeating content, but they also make it a little harder to manage your site because code and logic is contained in multiple files.

Keep user controls focused on a single task. Don’t create a user control that is able to display five different types of unrelated content with a property that determines what to display.

When you create user controls that contain styled markup, don’t hardcode style information like the CssClass for the server controls contained in the user control.



0 comments

Post a Comment