Custom Server Controls Why use them? ◦ Complete control over rendered HTML ◦ Better design-time support More difficult then User Controls
Custom Server Control Inherits System.Web.UI.Control ◦ Provides common properties and methods ◦ Ex. ID, ViewState, Controls Sometimes inherits from System.Web.UI.WebControls.WebContr ol ◦ Adds more features to implement styles ◦ Ex. Font, ForeColor , BackColor
Creating a Completely CustomControl Steps: 1. Create a new ASP.NET Server Control Project 1. Add a new class derive from Control 2. Override Render() method Use HTTPTextWriter to write the wanted HTML. 2. In your website: 1. Add reference to the custom control assembly (.dll) 2. Register assembly (.dll) in page 3. Register assembly (.dll) in web.config 4. Add the item to the Toolbox 5. Use in page
Styling custom Controls Better inherit WebControl Don’t overrride Render() Specify base tag of your control in constructor Add public properties Initialize Properties by overriding OnInit() Override AddAttributeToRender() to add the wanted attribute values Override RenderContent() to add the wanted content
Postback Data and ChangeEvent To process the data that’s posted to the page. Implement IPostBackDataHandler ◦ LoadPostData() called on page postback before any control event handling, to update control state ◦ RaisePostDataChangedEvent() to fire events ◦ The control must have a name attribute for postback to work
Lab #6 Create a custom control ◦ A labeled textbox with onTextChange event of the textbox and a Text attribute for the label. The data must be retained between postbacks. ◦ Use it in register.aspx
Report #6 What is control state of a control?
REFERENCES  Beginning ASP.NET 4 In C# 2010, Matthew Macdonald, Apress  Web Application Architecture Principles, Protocols And Practices, Leon Shklar And Richard Rosen, Wiley  Professional AS P.NE T 4 In C# And VB, Bill Evjen, Scott Hanselman And Devin Rader, Wiley  Pro ASP.NET In C# 2010, Fourth Edition,matthew Macdonald, Adam Freeman, And Mario Szpuszta, Apress