Building On Existing Controls

I l @ ve RuBoard

By overriding the Render() method of the base Control class, you can build very powerful controls. However, often you will find that there is an existing control that has most of the functionality you need. In this case, it is often much more efficient to simply inherit from the control that has most of your functionality. A common example of this kind of control would be a DropDownList that you would like to have pre- populated . Consider a form that has many yes or no questions on it. You might want to have a DropDownList with options for Yes, No, and a blank default option. You could create a custom control from scratch with this functionality, but you would not have any of the built-in functionality of the DropDownList Web control, such as handling PostBacks , unless you built it into your control. Rather than reinventing the wheel, you could just inherit from the DropDownList control itself, and add the necessary functionality needed to support your YesNoDropDownList . Listing 12.3 shows the complete source of our new control.

Listing 12.3 A YesNoDropDownList, YesNoDropDownList.cs
 namespace ASPNETByExample {     public class YesNoDropDownList : System.Web.UI.WebControls.DropDownList     {         public string Value         {             get             {                 return this.SelectedItem.Value.ToString();             }             set             {                 this.Items.FindByValue(value).Selected = true;             }         }         public YesNoDropDownList()         {             this.Items.Add("");             this.Items.Add("No");             this.Items.Add("Yes");         }     } } 

This control is a bit more complicated than our HelloWorld control, but is still pretty simple. We're still using our ASPNETByExample namespace, but this time instead of declaring our class as inheriting from Control, we are inheriting from System.Web.UI.WebControls.DropDownList . Note that, although we are not directly inheriting from Control , we still have access to all its behavior just as before, because DropDownList inherits (indirectly) from Control .

Just to make things a bit more interesting, I defined a Value property for this control, to make it easier to get or set the value of the DropDownList . The get just returns the value of the selected item, and the set searches for an item that matches what is passed into it and sets that item to be selected if it is found (the code right now will raise an exception if you try to set its value to an item that isn't in the list; we would add proper error handling to a production control). This property just makes it easier to work with our control, and also lets us set the value of the control declaratively on our ASP.NET page simply by adding a value="Yes" attribute to the control's declaration (as we shall see in a moment). Finally, the YesNoDropDownList() constructor is called whenever our control is created, and the constructor is responsible for creating the items in our list. In this case, it simply adds a blank option, a "No" option, and a "Yes" option, which will appear in that order on our page.

To test our control, we can use a simple ASP.NET page like the one listed in Listing 12.4.

Listing 12.4 YesNo.aspx
 <%@ Register TagPrefix="YN" Namespace="ASPNETByExample" Assembly="YesNoDropDownList" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html>     <body>         <form id="YesNo" method="post" runat="server">             Are we having fun yet?             <YN:YesNoDropDownList                 runat="server"                 id="fun"                 value="Yes"             />             <asp:Button Runat="server" text="Go" />             <hr />             <h1>                 <%=fun.Value%>             </h1>         </form>     </body> </html> 

This page looks very similar to our HelloControl.aspx page, with a few additions. First, since we want to use PostBacks , we need to make sure that our controls are all within a <form runat="server"> tag. Next, you'll notice that we have set a default value for our tag of " Yes " by using the Value property, which is not case sensitive in our ASP.NET page even though it is case sensitive in our C# class (which is why we can't rely on case distinctions for public properties in C# classes). Then, we've added a button so that we can test submissions of our page. When you click the button, you will see whatever your selection was in big letters at the bottom of the screen.


This example can be seen live online at

Extending existing Web controls can provide a lot of functionality, and as you saw here, it is fairly easy to accomplish. One of the advantages of using this technique is that you don't have to worry about handling PostBacks yourself. However, there are limits to what you can do with just one of these controls. Consider, for instance, if you wanted to be sure that someone always selected either Yes or No from your YesNoDropDownList . You would have to add a RequiredFieldValidator control (covered in Chapter 7) to every page that used the YesNoDropDownList to handle this. Although this solution would work, it would be much better if you could just include the RequiredFieldValidator functionality in with your control. One way to do this would be to override some more methods of the DropDownList , but again, why reinvent the wheel when there are already validation controls built into ASP.NET that we can use? The solution here is to use a composite control ”a custom control made up of two or more other controls.

I l @ ve RuBoard

Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: