Adding Properties and Methods to Your Control

   

Properties, by definition, are characteristics that you can modify at design time. Font color is a good example of a text property. Methods, by definition, are self-contained blocks of code referenced by a name . You can see an example of such a code block in the code in Listing 13.3, in the form of Output.Write() , where Write() is a method of the Output class.

The example in Listing 13.4 demonstrates how to write a control that exposes three custom properties (of type string , integer , and enum ) and then uses them to render some custom content based on how the properties are set. The syntax to add a property is as follows :

Listing 13.3 A Class with a Single Property
 public string SomeProperty  {      // Do stuff here      // Maybe have a get method      get      {          // Return the data for this property          return m_PropertyData;       }      // Maybe have a set method      set      {          // Set the data for this property          m_PropertyData = value;      }  } 
Listing 13.4 A Control That Exposes Three Properties
 namespace MyControl  {  public enum MsgSize  {      Small = 0,      Medium = 1,      Large = 2  };  [ShowInToolbox(true),      ToolboxData("<{0}:IterativeControl runat=server></{0}:IterativeControl>")]  public class IterativeControl : System.Web.UI.WebControls.WebControl  {      private String m_strText;      private MsgSize m_Size;      private int m_nIterations;      [Bindable(true),          Category("Appearance"),          DefaultValue("Isn't ASP.NET great?"),          Persistable(PersistableSupport.Declarative)]      public string Text      {          get          {              return m_strText;          }          set          {              m_strText = value;          }      }      [Bindable(true),          Category("Appearance"),          DefaultValue(MsgSize.Medium),          Persistable(PersistableSupport.Declarative)]      public MsgSize Size      {          get           {              return m_Size;          }          set          {              m_Size = value;          }      }      [Bindable(true),          Category("Appearance"),          DefaultValue(1),          Persistable(PersistableSupport.Declarative)]      public int Iterations      {          get          {              return m_nIterations;          }          set          {              m_nIterations = value;          }      }      protected override void Render(HtmlTextWriter output)      {          String strOutString;          if( Size == MsgSize.Small )          {              strOutString = "<H5>" + Text + "</H5>";          }          else if( Size == MsgSize.Medium )          {              strOutString = "<H3>" + Text + "</H3>";          }          else          {              strOutString = "<H1>" + Text + "</H1>";          }          for( int i=0; i<Iterations; i++ )          {              output.Write(strOutString);          }      }  }  } 

Now you need ASP.NET code that uses the new control so that you can see how it works. The example file shown in Listing 13.5 provides this code.

Listing 13.5 The ASP.NET Code That Uses the Second Control
 <%@ Register TagPrefix="CustomControl" Namespace="MyControl" %>  <html>     <body>        <form method="POST" runat="server">            <CustomControl:IterativeControl Text="Hello There"               MessageSize="Large" Iterations=3 runat="server"/>        </form>     </body>  </html> 

Adding Class Properties

In the example shown in Listing 13.6, you see a simple control that exposes a complex property (that is, a property that in turn contains other properties). Complex property support enables ASP.NET developers to organize values on a control hierarchically; that is, it enables them to nest controls within each other.

Listing 13.6 A Control That Exposes a Complex Property
 namespace MyControl  {  public class Formatter  {      public int m_nSize;      public String m_strColor;      public Formatter( int nSize, String strColor )      {          m_nSize = nSize;          m_strColor = strColor;      }  }  [ShowInToolbox(true),      ToolboxData("<{0}:SimpleSubProperty runat=server> </{0}:SimpleSubProperty>")]  public class SimpleSubProperty : System.Web.UI.WebControls.WebControl  {      private Formatter m_Format = new Formatter(3,"black");      private String m_strMessage;      [Bindable(true),          Category("Appearance"),          DefaultValue("Some Text"),           Persistable(PersistableSupport.Declarative)]      public string Message      {          get          {              return m_strMessage;          }          set          {              m_strMessage = value;          }      }      [Bindable(true),          Category("Appearance"),          DefaultValue("Some Text"),          Persistable(PersistableSupport.Declarative)]      public Formatter Format      {          get          {              return m_Format;          }      }      protected override void Render(HtmlTextWriter output)      {          String strOutString;          strOutString = "<font size=" + Format.m_nSize +              " color=" + Format.m_strColor + ">";          output.Write(strOutString);      }  }  } 

The code shown in Listing 13.7 uses the class properties to set the message to "Hello There" and the color to red.

Listing 13.7 The ASP.NET Code That Consumes the Example, Using the Properties by Setting the Message to "Hello There" and the Color to Red
 <%@ Register TagPrefix="CustomControl" Namespace="MyControl" %>  <html>     <body>        <form method="POST" runat="server">            < CustomControl:Formatter                Message="Hello There" Format-Color="red"               Format-Size="3" runat="server"/>        </form>     </body>  </html> 

Retrieving Inner Content

In the next example, you can see a simple control that retrieves its inner content (the HTML content contained with the <HTML> and <BODY> tags) within a page and renders it back on the client browser. Listing 13.8 is the source code for the control, and Listing 13.9 is the .aspx file that uses it.

Listing 13.8 The Code for the Control That Retrieves Inner Content
 namespace MyControl  {  [ShowInToolbox(true),      ToolboxData("<{0}:SimpleInnerContent runat=server> </{0}:SimpleInnerContent>")]  public class SimpleInnerContent : System.Web.UI.WebControls.WebControl  {     protected override void Render(HtmlTextWriter output)     {         if( ( HasControls() ) && ( Controls[0] is LiteralControl ) )         {            output.Write( "<H2>Your Message: " + ( (LiteralControl) Controls[0]).Text + "</ graphics/ccc.gif H2>" );         }     }  }  } 
Listing 13.9 The ASP.NET Code That Uses the Example
 <%@ Register TagPrefix="CustomControl" Namespace="MyControl" %>  <html>     <body>        <form method="POST" runat="server">            <CustomControl:SimpleInnerContent id="MyControl"               runat="server">               My Message Is Inside the Control Tag!!!!             </CustomControl:SimpleInnerContent >        </form>     </body>  </html> 
   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net