Developing User Controls


Developing User Controls

You have seen that ASP.NET provides a rich set of controls, including HTML, Web Server, and Validation controls. The fact that ASP.NET provides this list of controls and that these controls are prefixed with the "asp:" qualifier suggests that a capability should exist for users to define their own set of user controls. If you are thinking along these lines, you are absolutely right. ASP.NET does provide capability for users to define custom user controls. Take a look at how.

If you think about the Validated Email control that we defined earlier, a control like this would be a good candidate to be reused across many pages. You can do just that by creating another ASP.NET page, called Email.ascx, and including the text box control and the validation control in it.

 
 <asp:TextBox id="email" runat="server"/> <asp:RegularExpressionValidator id="EmailValidator" runat="server"      ControlToValidate="email"      ValidationExpression="^([a-z]*)\.([a-z]*)@mycompany.com">         You should enter your valid email address. </asp:RegularExpressionValidator> 

Cutting and pasting into a different file with a special extension (.ascx ”ASP.NET control) immediately provides the reusability of this control to other pages.

 
 <%@ Page Language="C#" %>  <%@ Register TagPrefix="my" TagName="Email" src="Email.ascx" %>  <html> <head> </head> <body>     <form runat="server">         <table border="1">                 <tr>                     <td>Email Address</td>                     <td>  <my:Email runat="server"/>  </td>                 </tr>         </table>     </form> </body> </html> 

Examine again the bold areas of the preceding code snippet. You register the control by using the Register directive, provide it with the custom prefix "my", and use the control by simply including the prefix and the control name . This cut and paste is really the core of creating ASP.NET custom controls using .ascx files. Imagine the benefit this can provide from reusability and abstraction with common business user interface controls such as addresses, contact information, user profile, login screen, password request, and the like.

User Control Properties

Similar to using parameters for standard HTML/ASP.NET controls to customize them, you may want to apply the same capabilities to your own user control, as well. Take a look at the modified Email control that follows :

 
 <script language="C#" runat="server">   private String _company;   public String Company   {     get {        return _company;     }     set {        company = value;        EmailValidator.ValidationExpression="^([a-z]*)\.([a-z]*)@"+_company;     }   } </script> <asp:TextBox id="email" runat="server"/> <asp:RegularExpressionValidator id="EmailValidator" runat="server"       ControlToValidate="email"       ValidationExpression="^([a-z]*)\.([a-z]*)@mycompany.com">          You should enter your valid email address. </asp:RegularExpressionValidator> 

The preceding code adds a property called Company to your Email control and can be used to fine tune the execution of the control based on its usage. Of course, the control logic stays the same; only its usage instance gets modified. The fact that an ASP.NET page is converted into a class is highlighted in this scenario as well.

 
 <%@ Page Language="C#" %> <%@ Register TagPrefix="my" TagName="Email" Src="Email2.ascx" %> <html> <head> </head> <body>     <form runat="server">         <table border="1">                 <tr>                     <td>Email Address</td>                     <td>  <my:Email runat="server" Company="hiteshseth.com"/>  </td>                 </tr>         </table>     </form> </body> </html> 

User controls can also be built purely in .NET code (instead of using .ascx files). This also becomes the basis of the ASP.NET third-party controls ecosystem; chances are that if you are looking for a particular control and the set of ASP.NET controls don't fit your requirements, one of the third-party controls will.



Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

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