ASP.NET 4.0Julie IskanderMSC. Communication and Electronics
Lecture Outlines   Custom Server Controls
Custom Server Controls   Why use them?    ◦ Complete control over rendered HTML    ◦ Better design-time support   More d...
Custom Server Control   Inherits System.Web.UI.Control    ◦ Provides common properties and methods    ◦ Ex. ID, ViewState...
Creating a Completely CustomControl   Steps:    1. Create a new ASP.NET Server Control       Project     1. Add a new cla...
Step 1
Convert from an HTML to an aspx page
The Rendering Process
Styling custom Controls   Better inherit WebControl   Don’t overrride Render()   Specify base tag of your control in   ...
Convert from an HTML to an aspx page
View State To save and retrieve property values  in the view state To store information of the control  between postbacks.
Convert from an HTML to an aspx page
Postback Data and ChangeEvent To process the data that’s posted to  the page. Implement IPostBackDataHandler    ◦ LoadPo...
Convert from an HTML to an aspx page
Triggering a PostBack Is supported by a JavaScript function  named __doPostBack(); Override AddAttributesToRender()    ...
Convert from an HTML to an aspx page
Controls Property   A collection of childern controls to be    added to the control rendered with    RenderChild()
Convert from an HTML to an aspx page
Lab #6   Create a custom control    ◦ A labeled textbox with onTextChange      event of the textbox and a Text attribute ...
Report #6   What is control state of a control?
REFERENCES [1] Beginning ASP.NET 4 In C# 2010, Matthew  Macdonald, Apress [2] Web Application Architecture Principles,  ...
ASP.NET Lecture 6
ASP.NET Lecture 6
ASP.NET Lecture 6
ASP.NET Lecture 6
ASP.NET Lecture 6
ASP.NET Lecture 6
ASP.NET Lecture 6
Upcoming SlideShare
Loading in …5
×

ASP.NET Lecture 6

1,111 views
1,047 views

Published on

This lecture presents how to create custom server controls

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,111
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Create a new project  ASP.NET Server Control HTMLTextWriter class  to write raw HTML, provides methods to manage style attributes and tags
  • SimpleControl
  • RenderControl is not overriden
  • ControlWithPropertyControlWithExceptionHandlin
  • ControlWithViewState
  • LoadPostData tell ASP.NET whether a change happened and an event is required.
  • For a custom textbox
  • ControlWithPostBack
  • ControlWithPostBack
  • GreetControl
  • ASP.NET Lecture 6

    1. 1. ASP.NET 4.0Julie IskanderMSC. Communication and Electronics
    2. 2. Lecture Outlines Custom Server Controls
    3. 3. Custom Server Controls Why use them? ◦ Complete control over rendered HTML ◦ Better design-time support More difficult then User Controls
    4. 4. 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
    5. 5. 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
    6. 6. Step 1
    7. 7. Convert from an HTML to an aspx page
    8. 8. The Rendering Process
    9. 9. 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
    10. 10. Convert from an HTML to an aspx page
    11. 11. View State To save and retrieve property values in the view state To store information of the control between postbacks.
    12. 12. Convert from an HTML to an aspx page
    13. 13. 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
    14. 14. Convert from an HTML to an aspx page
    15. 15. Triggering a PostBack Is supported by a JavaScript function named __doPostBack(); Override AddAttributesToRender()  Add a javascript event attribute to the tag  The value is set using Page.ClientScript.GetPostBackEventReference ()
    16. 16. Convert from an HTML to an aspx page
    17. 17. Controls Property A collection of childern controls to be added to the control rendered with RenderChild()
    18. 18. Convert from an HTML to an aspx page
    19. 19. 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
    20. 20. Report #6 What is control state of a control?
    21. 21. REFERENCES [1] Beginning ASP.NET 4 In C# 2010, Matthew Macdonald, Apress [2] Web Application Architecture Principles, Protocols And Practices, Leon Shklar And Richard Rosen, Wiley [3] Professional AS P.NE T 4 In C# And VB, Bill Evjen, Scott Hanselman And Devin Rader, Wiley [4] Pro ASP.NET In C# 2010, Fourth Edition,matthew Macdonald, Adam Freeman, And Mario Szpuszta, Apress

    ×