Title: ASP.NET Server Controls
1ASP.NET Server Controls
- Presented By
- Robin Lilly
- iLogbook
2Agenda
- Introduction
- Basic control authoring topicsImplementing
properties - Rendering
- Raising Events
- Extending existing controls
- Compositing existing controls
3Agenda
- Build some real world examples
- 2 ADA controls
- Label
- ltlabel forltClientIDgtNamelt/labelgt,
- Textbox
- Inherited textbox that implements onfocus
onblur - Inspect a Shopping Cart
- Composite Control
- Inspect code
- CreateChildControls()
- BackButton prevention via session state
- Properties
- UI Type Editor
- Events
- State Management View State and Session
4What Is A Server Control?
- A server control is a .NET component that is used
to generate the user interface of an ASP.NET Web
application. - It is implemented as a managed class deriving
directly or indirectly from the
System.Web.UI.Control base class.
5What Is A Server Control?Speaking More
Practically
- A Web user interface element
- Renders into HTML, script or a different markup
format - Allows customization of rendering
- A Web user interface component
- Exposes properties, events and methods and is
programmable - Provides higher level abstractions
- Performs post-back processing
- Handles differences between browsers
- Consistent programming model
- A RAD component that offers adesign-time
experience
62 Ways To AuthorServer Controls
- User Controls
- Simple, declarative authoring model (.ascx file)
- Scoped to a single application
- Well suited to static content and layout
- Custom or Compiled Controls
- Code-based authoring model (.cs or .vb class
file) - Easily shared across applications
- Well suited to dynamic or programmatic generation
of content and layout - More complex, but also more capabilities
7Setting up your Solution
- Use a WebControlLibrary project to create custom
compiled controls - Initialize the Version attribute
- Add a Web app to test controls
- Solution gt Add New gt Web Application
- Add a ToolBox reference
- Customize Toolbox gt Browse
8Rendering
- Override Render to representative markup
- Use HtmlTextWriter API to implement rendering
logic - public override void Render(HtmlTextWriter
writer) - writer.RenderBeginTag(HtmlTextWriterTag.Span)
- writer.Write(Text)
- writer.RenderEndTag()
9Custom Toolbox Glyph
- Bitmap file with same base name as control class
in the same namespace - TextBoxADA
- Build Action Embedded Resource
- Image properties
- File type supported is bitmap
- 16x16 pixels
- Lower-left pixel is used to determine
transparency - Adds an extra professional touch
10Building Controls In Visual Studio .NET
- Setting up your Solution
- Choosing a Tag Prefix
- Choosing a Toolbox Glyph
- Debugging your Control
11What is the ADA?
- Defines web accessibility
- American Disabilities Act or US Section 508
compliance http//www.section508.gov/
http//www.usdoj.gov/crt/ada/adahom1.htm - Universities and Governments are required to
implement.
12Some Requirements for ADA
- Input controls must have a description of the
input item.
13Requirements for ADA
- Lets see it in action http//localhost/serverregis
tration/registration2.aspx
14ADALabel
- Simple, minimal control
- Renders a ltlabel forclientidgtNamelt/labelgt
- Inherits from LiteralControl
- Metadata on properties
- Demonstrates state management
- Uses ViewState in property implementation
- Demonstrates basic rendering
- Render Method
15ADATextBox
- Simple, minimal control
- Renders in TextBox
- onfocus"if(this.value'Enter Serial
Number')value''" - onblur"if(this.value'')value'Enter Serial
Number' - Inherits from LiteralControl
- Inherits from TextBox
- Demonstrates state management
- Uses ViewState in property implementation
- Demonstrates basic rendering
- AddAttributesToRender method
16ADATextBox
- Metadata on properties
- Declarative way of specifying behavior
- Property editing in property browser
- Property persistence
- Conversion of types to/from strings
- Metadata can be applied to events, methods and
types as well
17ADAInitialValue Property
Bindable(true), Category("Appearance"),
DefaultValue(""), Description(...") public
string InitialValue get object o
ViewStateInitialValue " if (o
null) return String.Empty
else return (string)o
set ViewStateInitialValue "
value this.Text value
18 demo
ADATextBox ADALabel
State managed properties, Rendering
19Composite Control
- Contains Table, TableRows and TableCells,
Textbox, Button - Implements the standard composite control pattern
- Implements INamingContainer
- Overrides Controls property to ensure child
controls - Overrides CreateChildControls to implement logic
of creating child controls
20Why Our own Shopping Cart at UTEP
- Needed to call our own credit card api TOUCHNET
internally - Needed to be reusable by all programmers
- Handle financial accounting transactions
differently. - Nothing existed that solved the problems
21Shopping Cart Composite
22CreateChildControls
23AddDataGrid
24AddDataGrid (cont)
25AddDataGrid (cont)
26Event Implementation
- Standard event pattern has two parts
- Event declaration
- OnltEventgt protected virtual method
Public Event FinishShopping as ShoppingEventHandle
r Protected Sub OnFinishShopping(E as
ShoppingEventArgs) RaiseEvent
FinishShopping(Me,E) End Sub
27Events Implemented
- SuccessShopping
- CancelShopping
- FinishShopping
28ShoppingEventArgs
29Shopping Session Table
- Exists to prevent Recharges
- Hitting the Back in Browser and then recharging
the card for the same purchase
30Session State Table
- CreateSessionShoppingCart
31Shopping Session Table
- IsSessionShoppingCartExist
32Session State Procedures
- DeleteOldShoppingCartSessions
- delete SessionShoppingCart
- where CreateDate lt DateAdd(day,-7,GetDate())
33Add Item to Cart
34 demo
Shopping Cart
35CartData
36MySession
37EmptyCart
- Used after Finish Shopping?
38AddCreditCard
39AddCreditCard
40AddCreditCard (cont)
41AddCreditCard
42AddCreditCard(cont)
43Successful Charge
44Charge Credit Card Button
45(No Transcript)
46Some of Public Style Properties
- ItemHeaderText - get/set
- DescriptionHeaderText get/set
- QuantityHeaderText get/set
- PriceHeaderText get/set
- AmountHeaderText get/set
- DisplayQuantity get/set
- DisplayGrid get/set
- DataGridCSSClass get/set DataGrid Style
- ValidateCSSClass get/set Validator Styles
- RequireCSSClass get/set Required Validator
Styles - DisplayErrorStar get/set Bool
- DataGridBackColor get/set Color
- DataGridHeaderColor get/set Color
- DataGridForeColor get/set Color
47Debugging Your Control
- Set a breakpoint in your control runtime
- Set the Web App as Startup Project
- Set a Startup Web page
- F5
- Use your control in the running page
48State Management
- Session vs. View State
- View State
- Override various methods to perform custom state
management - TrackViewState
- SaveViewState
- LoadViewState
- Required any time you have nested objects like
Styles or collection properties - Call on IStateManager implementation of these
objects to include their state as part of
Controls state
49Key Points
- Controls provide an abstraction and reusability
mechanism for web apps - ASP.NET provides a rich framework for creating
server controls - Create specialized derived controls to make
incremental changes toexisting controls - Use composition to leverage existing controls in
more specialized scenarios - Think about your Markup!!!!!
50Essential Resources
- Developing Microsoft ASP.NET Server Controls and
Components
- ISBN 0-7356-1582-9
- Download
- Source Code
- Design Guidelines
- http//www.microsoft.com/mspress/books/5728.asp
- ASP.NET Forums at http//www.asp.net/forums
- MSDN and .NET Framework SDK Documentation
51 Download
Samples
52Questions And Answers
53Server Control
54(No Transcript)
55(No Transcript)