1 / 18

ASP.NET Custom Controls

ASP.NET Custom Controls. Custom Controls. Encapsulate Rich Functionality Reusable Redistributable. Types of Custom Controls. User Controls Server Controls. User Controls. Easy to create Easy to use Lack support for design-time features Scoped to a single application

nika
Download Presentation

ASP.NET Custom Controls

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP.NET Custom Controls

  2. Custom Controls • Encapsulate Rich Functionality • Reusable • Redistributable

  3. Types of Custom Controls • User Controls • Server Controls

  4. User Controls • Easy to create • Easy to use • Lack support for design-time features • Scoped to a single application • Very similar to creating an ASPX page

  5. Server Control • Harder to author • Done through code and not visually • Highly customizable design-time experience • Distributable to many applications

  6. Creating Server Controls • Server Controls are derived from • System.Web.UI.Control • System.Web.UI.WebControls.WebControl • From any other control

  7. System.Web.UI.Control • Base class for all controls • Doesn’t implement a user interface • Use this when you are creating nonvisual controls

  8. System.Web.UI.WebControls.WebControl • Derives from System.Web.UI.Control • Adds User Interface features for handling • Most controls will derive from WebControl

  9. All other controls • Used to set UI standards for a development group

  10. Design-Time Attributes • Properties • Get/Set methods • Initialize the control • Attributes • Display information about properties • Categorize properties

  11. UniqueID • Unique identifier of a control • Must be assigned to a control for the run-time methods to work

  12. Run-Time Methods • CreateChildControls • Signals a composite control to create child controls • Dispose • Enables a controls to perform cleanup • LoadViewState • Customized state information restoration • SaveViewState • Customized state information persistence • Render • Send content to a browser

  13. Render • Ultimate goal of a control is to display HTML in a browser • ASP.NET runtime will traverse all the controls in an ASPX page and call the render method on each one • Place the HTML that will be send in to the browser in the Render method

  14. Generate HTML in Render • Generate raw HTML using the Write method of the HtmlTextWriter • Use the HtmlTextWriter helper functions • RenderBeginTag • RenderEndTag • AddAttribute (prior to RenderBeginTag) • AddStyleAttribute (prior to RenderBeginTag) • Client JavaScript can be rendered in the same way

  15. Respond To Client Post • Must Implement System.Web.UI.IPostBackDataHandler • LoadPostData • Updates the state of the control as a result of a postback • Returns true if the state has changed, otherwise false • RaisePostDataChangedEvent • Will be called if LoadPostData returned true

  16. Capture Postback Events • Implement IPostbackEventHandler • RaisePostBackEvent • Will be called when a postback event is caused • e.g. When a button is clicked • By default only the button and image button cause a postback • Can add a postback method to a control by using some simple client side JavaScript, the Render method and the GetPostBackEventReference method • Generating Client-Side Script for Postback  (MSDN)

  17. Maintaining State • Each server control has a ViewState property • Allows it to participate in state management • Can be used instead of a private field

  18. Review • User Controls • Server Controls

More Related