Title: Web User Controls
1Web 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
2About Your Speaker
- Name Joseph White - JoeCasa1_at_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.
3Web User Controls
- First, Lets 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.
4Web 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.
5Web User Controls
- What they are NOT
- They aint server controls.
- Easily distributed or shared across multiple web
projects. - Something you would want to package and sell to
the public.
6Web 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.
7Web User Controls
- Lets take a peek at what they look like in
Visual Studio
8Web User Controls
- Two part entry into the aspx page.
- Part 1 The declaration.
- lt_at_ Register TagPrefix"uc1" TagName"PageHeader"
Src_PageHeader.ascx" gt - 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 Lets the page know where it can find the
ascx file that goes with the control.
9Web User Controls
- Part 2 The actual control tag.
- ltuc1pageheader id"_PageHeader1" runat"server"
/gt
Required if manipulating on Server
TagName
TagPrefix
Unique instance identifier
10Lets Make a Simple Control
11Web 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.
12ASP.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.
13Web 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.
14Web User Controls
- Complex regular expression
- (0?1-910-2)/(0-2?0-930-1)/((12)\d3
\d2) (0-10-920-3)0-50-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.
15Validation Controls Demo
http//www.ilmservice.com
16Web User Controls
- Working with style sheets.
- Style Sheet Advantage
- Since the controls are site specific you dont
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.
17Web 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
18Web 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.
19Web User Controls
- Conclusions
- Cons
- Doesnt 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.
- Cant package and sell as a stand alone DLL.
20Web User Controls
- Thank you for your attention.