Creating a DropDownLabel Server Control
page 2 of 4
by Matthew Mondok
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 22093/ 53

A [Very] Short Overview of a Server Control

Server controls, besides HTML controls, are one of the basic building blocks for creating ASP.NET applications.  Ultimately, all the standard ASP.NET server controls render out to HTML, but some contain more functionality than others.  For proof, just do a View Source of any aspx page.  In order to create server controls, one must understand exactly how they are rendered to the page.  Server controls are extensions of the tools that we as developers are given.  ASP.NET has given us a fantastic library, but we must put it all together.  A custom server control usually extends one of the standard classes found in the System.Web.UI.WebControls namespace.  Although we could extend of of the classes within the System.Web.UI.HtmlControls namespace, it is not recommended because we lose many additional properties provided by the base WebControl class.  Within the WebControls library, we can choose to derive from many different controls, and in this article I will use the DropDownList class.

The basic methods that my server control uses are Render and PreRender from the WebControl class, and RegisterStartupScript and RegisterClientScriptBlock from the Page property.  Each of these will be explained briefly, but I would suggest doing further reading on the MSDN website to get a full understanding of what is actually happening.

Understanding Render and PreRender

The DropDownLabel control overrides two methods, Render and OnPreRender.  In general, the Render method writes the required HTML to the page.  The OnPreRender method adds any necessary data to the ViewState of the page, prior to writing out the HTML or rendering the content.  Typically, any DHTML should be generated and registered in the OnPreRender method.

Understanding RegisterStartupScript and RegisterClientScriptBlock

Two other methods that we will be using are available from the Page property.  These two methods are RegisterStartupScript and RegisterClientScriptBlock.  The RegisterClientScriptBlock method emits any script directly before the closing server-side form tag.  The RegisterClientScriptBlock method emits any script directly after the opening server-side form tag.  RegisterClientScriptBlock is perfect for DHTML functions because it cannot access any form objects, but it is great to get a function ready to be accessed.  Conversely, the RegisterStartupScript runs after the form is initialized and it can be called immediately without throwing DHTML errors.  Basically, if I wanted to call some script that would run equivalent to the OnLoad event, I would use the RegisterStartupScript method.  If I wanted to add a DHTML function to the page, I would use the RegisterClientScriptBlock method. 

All in all, understanding the mechanics of how the events that fire before a page is rendered is imperative to creating server controls.  Obviously, a quality server control cannot be created if the developer does not understand what happens at the presentation layer.  That being said, I will now explain the steps to creating the control.

View Entire Article

User Comments

No comments posted yet.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2023  |  Page Processed at 2023-09-29 8:10:59 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search