1 / 11

ASP.NET User Controls

ASP.NET User Controls. Make your own controls by grouping other controls. What is a User Control?. A user controls is a grouping of other server controls into a single-container control User controls are composite A user control contains child controls. How to create a User Control?.

hedya
Download Presentation

ASP.NET User Controls

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP.NET User Controls Make your own controls by grouping other controls ASP.NET User Controls

  2. What is a User Control? • A user controls is a grouping of other server controls into a single-container control • User controls are composite • A user control contains child controls ASP.NET User Controls

  3. How to create a User Control? • Make a new folder ”Controls” • Create a New Item -> User Control • Filename.ascx • User controls are designer friendly • You can drag-and-drop child controls from the Visual Studio Toolbox to the control ASP.NET User Controls

  4. When to create a User Control? • A User Control is useful, if you have a set of controls that appears on many pages. • You aggregate the individual controls in a new user control • The user control can then be used in a number of pages ASP.NET User Controls

  5. Using a user control • Using a User Controls is like using any other server control • User controls can be used wherever you can user other kinds of controls, like • Web forms • Master pages • Other (bigger) user controls • User control must be registered • Register in single page <%@register ….%> • Register in web.xml ASP.NET User Controls

  6. User control registration • To use a User Control the User Control must be registered. • Registration can be done in two places • In the file using the User Control • <%@ Register src="/Controls/Banner.ascx" tagname="Banner" tagprefix="uc1" %> • <uc1:Banner ID="Banner1" runat="server" DisplayDirection="Horizontal" /> • In web.xml • Site-Wide registration ASP.NET User Controls

  7. Site-Wide registration of user controlsin web.config <configuration> … <system.web> <pages> <controls> <add tagPrefix="mine" tagName="Banner" src="/Controls/Banner.ascx"/> </controls> </pages> … <system.web> <configuration> ASP.NET User Controls

  8. User Control Properties • User Controls can have properties • Properties are declared in the code-behind of the user control • Set in the page that uses the User Control • Declaratively from the page using the User Control • Programmatically from the code-behind of the page using the User Control • Get the value in the code-behind ASP.NET User Controls

  9. User control events • User controls can have events • Just like ordinary controls • Example: Events from sub-controls can be propagated to the user control • Called ”Event Bubbling” • Event arguments can be empty, or carry data. • Example: usercontrol/loginusercontrol ASP.NET User Controls

  10. Best practices • Don’t overuse user controls • User controls encapsulate repeating content • UI + code spread over more files • A user controls should focus on a single task • Don’t hardcode style info (CSS) in a user control • Make a CssClass property for the user control ASP.NET User Controls

  11. Further reading, etc. • Imar SpaanjaarsBeginning ASP.NET 4 in C# and VB, Wrox 2010 • Chapter 8 User Controls, page 271-296 • ASP.NET - 10 - User controls (video 1:19 min) • http://www.youtube.com/watch?v=NDWLtFvqQaY • http://www.5min.com/Video/How-to-Use-ASPNET---Tutorial-10---User-Controls-63590289 • Chris Peels Create a Custom User Control in ASP.NET (video 27 min, VB), 2007 • http://www.asp.net/general/videos/how-do-i-create-a-custom-user-control-in-aspnet • Bill Evjen et al. Professional ASP.NET 4 in C# and VB, Wrox 2010 • Chapter 25 User and Server Controls, page 979-987 • George Shepherd Microsoft ASP.NET 4 Step by Step, Microsoft Press 2010 • Chapter 5 Composite Controls, page 110-117 ASP.NET User Controls

More Related