aspxtreme

Custom Server Control Syntax

ASP.NET Syntax   ASP.NET Page Syntax


Defines user controls and user-authored server controls declaratively in a Web Forms page.

<tagprefix:tagname
   id="accessID" 
   attributename = "value"
   attributename-propertyname = "value"
   eventname = "eventhandlermethod" 
   runat="server" />

or 

<tagprefix:tagname id="accessID" runat="server"> </tagprefix:tagname>

Attributes


tagprefix An alias for the fully qualified namespace of the control. Aliases for user-authored controls are declared with the @ Register directive.
tagname The name of the class that encapsulates the control.
id A unique identifier that enables programmatic reference to the control.
attributename The name of the attribute.
propertyvalue The value to assign to attributename.
propertyname The name of the property being defined.
supropertyvalue The value to assign to propertyname.
eventname The event name of the control.
eventhandlermethod The name of the event handler method defined in the code for the Web Forms page.

Remarks

The opening tags of these elements must include a runat="server" attribute/value pair. If you want to enable programmatic referencing on the control, specify a unique value with the id attribute. You must include an @ Register directive for each custom server control assembly and user control that you declare in a page. If you do not, a compilation error occurs.

Any properties that you have authored on a custom server control can be exposed declaratively in the opening tag of the server control. Simply declare the property as an attribute and assign it a value. For example, if you create a custom text box control with a width property, declaring width = "50" in the opening tag of the control sets the server control's display width to fifty pixels.

In some cases, attributes may be objects that have their own properties. In this case, include the property name in the declaration. For example, if you create a custom text box control that includes a font attribute, it can include a name property. You can then declare the property in the opening tag of the server control as font-name = "Arial". For more information about developing custom server control with properties, see Properties in ASP.NET Server Controls.

You can also declare events with custom server controls and user controls in the same way you would with any ASP.NET server control. Simply specify the event binding in the opening tag of the server control with an attribute and value. For more information about writing event handlers, see Defining Web Forms Event-Handling Methods. For more information on authoring custom server controls that support events, see Web Forms Events and Handlers.

You can also use and develop custom server controls that support inline templates. For details on how to declare templates in a custom server control, see Server Control Inline Template Syntax. To learn how to author custom server controls that support inline templates, see Developing Templated Controls.

Syntax Example

The below code snippet demonstrates how you can register and declare a custom server control in a Web Forms page. The control's tag prefix is Custom and the class it creates is found in the CustomWebFormsControls namespace. It is declared using the Custom:MyButton tag in the body of the page.

<%@ Register tagPrefix = "Custom" Namespace = "CustomWebFormsControls" Assembly = "CustomControls" %>
<html>
   <script language="C#" runat=server>
      private void Button_Click ( Object sender, EventArgs e ) {
      TextBox.BackColor = System.Drawing.Color.Green;
      TextBox.Text = "You clicked the button";
      }
   </script>
   <body>
      <form method = "POST" action = "MyButton.aspx" runat=server>
         Here is our custom button.<br>
         <Custom:MyButton id="Button"  onClick =
                       "Button_Click" runat=server />

            <br>
            <br>
         <asp:TextBox id="TextBox" Text = "Click
                      the button" Width = "200"  BackColor =
                      "Cyan" runat=server />

         <br>
      </form>
    </body>
</html>
  C# VB

See Also

Introduction to Web Forms



Books and more ...


Suggested Reading

Need a break ?



Previous page Back to top Next page

Check out related books at Amazon

© 2000-2008 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support aspxtreme