210 likes | 457 Views
Web User Controls. This presentation will cover the basics of defining, creating and using a web user control. Presented to Twin Cities .NET user group By Joseph White Chief Architect, Eden Systems International http://www.EdenSI.com August 5 th , 2004. About Your Speaker.
E N D
Web User Controls This presentation will cover the basics of defining, creating and using a web user control. Presented to Twin Cities .NET user group By Joseph White Chief Architect, Eden Systems International http://www.EdenSI.com August 5th, 2004
About Your Speaker • Name: Joseph White - JoeCasa1@Yahoo.com • Occupations: • Chief Software Architect for Eden Systems International • .NET mentor and architect (part time contracting available through ILM, your .NET solution provider) • Self Proclaimed “Lazy Programmer” • Experience: • Working with .NET for over 3 years, developing software solutions, teaching and mentoring for over a decade.
Web User Controls • First, Let’s resolve the name ambiguity • User Controls • Could be confused with windows forms controls. • Server Controls • Not ambiguous if you know what you are talking about. • Custom Controls • Just as confusing as user controls. • Web Controls • Used interchangeably to reference either web user controls or server controls.
Web User Controls • What they are: • Similar in functionality to ASP include files but much better. • Encapsulate HTML and code into smaller functional units. • Built similar to web forms but hosted on a page as an object. • Reusable within the web project that hosts them.
Web User Controls • What they are NOT • They ain’t server controls. • Easily distributed or shared across multiple web projects. • Something you would want to package and sell to the public.
Web User Controls • Why not just use an old fashion include? • Include files are not encapsulated objects. • Potential variable name conflict with host web page or other instances of the “included” code. • Can not be programmatically manipulated by host page. • Web user controls contain properties and raise events which provides fluid interaction between the control and the hosting page. • Why not create a server control? • Too much work.
Web User Controls Let’s take a peek at what they look like in Visual Studio
Web User Controls Two part entry into the aspx page. Part 1: The declaration. <%@ Register TagPrefix="uc1" TagName="PageHeader" Src=“_PageHeader.ascx" %> TagPrefix: This is like a namespace in case you want to include other controls with the same name. Usually defaults to uc1. TagName: Again its only significance is to help uniquely identify the control on the page. Usually defaults to the name of the class. Src: Let’s the page know where it can find the ascx file that goes with the control.
Web User Controls Part 2: The actual control tag. <uc1:pageheader id="_PageHeader1" runat="server" /> Required if manipulating on Server TagName TagPrefix Unique instance identifier
Web User Controls Things to Note: • The control does not declare itself in the code behind. • Use Page.FindControl. • Manually type in the declaration. • The properties do not show up in the property sheet. • The design view can be drastically different from the runtime view. • In fact, when placed in a repeater control, they become invisible in the designer. • A web user control can host another web user control.
ASP.NET Validation ControlsSpecial kind of server controls used for validating input values entered in other server controls. A variety of validation controls allow us to require input, check for formats, compare with other fields, and perform custom operations.
Web User Controls Using Validators in a web user control • Encapsulate Complex Validation Logic • Validators in the user control will work with the validation summary control on the page. • Because the web user control is web project specific it is easy to give validators a consistent look throughout the application.
Web User Controls Complex regular expression: (0?[1-9]|1[0-2])/([0-2]?[0-9]|3[0-1])/((1|2)\d{3}|\d{2}) ([0-1][0-9]|[2][0-3])[0-5][0-9] Evaluates mm/dd/yy or mm/dd/yyyy with the leading 0 on the month or day being optional. Then it ends with HHMM where leading zero is not optional.
Validation Controls Demo http://www.ilmservice.com
Web User Controls Working with style sheets. • Style Sheet Advantage: • Since the controls are site specific you don’t have to worry about all sorts of properties to handle styles. • Style Sheet Disadvantage: • Since the style sheet is declared at the page level, your control (ascx file) may not look quite right in the VS designer.
Web User Controls Working with client side script • Best Practice for large script blocks is to use an include file. • Since a control may be included more than once on a page, It is important to make sure the script functions are only included once. • Use Page.RegisterClientScriptBlock and Page.IsClientScriptBlockRegistered
Web User Controls Conclusions Pros: • Easy to create: • Same programming model as ASP.NET Page • Code Behind • Page Load event • Post Back events • Encapsulate complex expressions, functions and html code. • OO approach avoids the pitfalls of copy and paste or old fashion includes.
Web User Controls Conclusions Cons: • Doesn’t play well with the VS designer. • Renders as a gray box in design view. • Exposed properties not available in property sheet. • Does not declare itself in the code behind. • Difficult to share across multiple web projects. • Can’t package and sell as a stand alone DLL.
Web User Controls Thank you for your attention.