.NET Samples - ASP.NET Control Authoring
This section includes the following samples:
Simple Sample
SimpleInnerContent Sample
Template1 Sample
SimpleSubProperty Sample
Label Sample
SimpleProperty Sample
CustomParse2 Sample
CustomParse1 Sample
Repeater1 Sample
NonComposition1 Sample
NonComposition2 Sample
Repeater2 Sample
Composition3 Sample
Composition2 Sample
Composition1 Sample
Simple Sample
This sample demonstrates a simple control that renders a message string.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI
Classes used in this sample:
Control; HtmlTextWriter; TextWriter; Assembly
SimpleInnerContent Sample
This sample shows a custom control named SimpleInnerContent
that renders text added between its tags by checking whether a LiteralControl has been added to its Controls collection. If so, it retrieves the Text property of the LiteralControl, and appends it to its output string.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI
Classes used in this sample:
Control; Message; Literal; LiteralControl; TextWriter; HtmlTextWriter; Assembly
Template1 Sample
This sample shows a simple templated control and an ASP.NET page that uses it. The ASP.NET page framework allows control developers to author controls that separate the user interface from the control logic through the use of templates. Page developers can customize the presentation of the control by providing the UI as parameters between template tags.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI
Classes used in this sample:
Control; Message; TemplateControl; String; Container; Page; LiteralControl; Literal; Assembly
SimpleSubProperty Sample
This sample defines a custom control named SimpleSubProperty
that has a property of type Format. The Format class has two primitive properties, Color and Size, which in turn become subproperties of SimpleSubProperty
. If a class A has a property whose type is class B, then the properties of B (if any) are called subproperties of A.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI
Classes used in this sample:
Form; Control; String; Message; HtmlTextWriter; TextWriter; Assembly
Label Sample
This sample shows a custom control, Label
, with two properties, Text and FontSize, that are saved in State.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI
Classes used in this sample:
Label; Font; Control; Button; Message; Object; String; EventArgs; Assembly; TextWriter; HtmlTextWriter
SimpleProperty Sample
This sample shows how to add simple properties to a control that correspond to primitive data types such as integer, Boolean, and string.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI
Classes used in this sample:
Message; Control; String; HtmlTextWriter; TextWriter; Assembly
CustomParse2 Sample
This sample defines a custom control builder that overrides the GetChildControlType method inherited from ControlBuilder. This method returns the type of the control to be added, and can be used to decide which controls will be added. In the example, the control builder adds a child control only if the tag name is <customitem>. The code for the control is very similar to the previous example, except for the addition of the custom attribute.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI; System.Collections
Classes used in this sample:
Control; Message; ControlBuilder; String; Type; Array; ArrayList; Object; ControlBuilderAttribute; HtmlTextWriter; TextWriter; Attribute; Assembly
CustomParse1 Sample
This sample defines a custom control named CustomParse1
that overrides the default parsing logic. When a child control of a certain type is parsed, it adds it to a collection. The rendering logic of CustomParse1
is based on the number of items in that collection. A simple custom control, Item
, is also defined in the sample.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI; System.Collections
Classes used in this sample:
Control; Message; String; Object; ArrayList; Array; HtmlTextWriter; TextWriter; Assembly
Repeater1 Sample
This sample shows a more complex use of templates to create a data-bound control. The Repeater
control defined in this sample is similar to the System.Web.UI.WebControls.Repeater control.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI; System.Collections
Classes used in this sample:
Control; Repeater; RepeaterItem; TemplateControl; Container; Object; EventArgs; ArrayList; Binding; Array; DataBinding; Assembly; Page
NonComposition1 Sample
This sample demonstrates a custom control that renders an input box, and reads data entered by the user. It implements the IPostBackDataHandler interface and has one property, Value.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI; System.Collections; System.Web.UI.WebControls; System.Collections.Specialized
Classes used in this sample:
Control; EventArgs; Object; HtmlTextWriter; TextWriter; WebControl; Assembly; NameValueCollection; String
NonComposition2 Sample
This sample defines a custom control, NonComposition2
, that builds on the previous example, NonComposition1
. In addition to the interface provided by NonComposition1
, it renders two HtmlButton controls that generate client-side script to cause a postback event when clicked.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI; System.Collections; System.Web.UI.WebControls; System.Collections.Specialized
Classes used in this sample:
Control; Page; String; NameValueCollection; TextWriter; HtmlTextWriter; Assembly; EventArgs; EventHandler; WebControl
Repeater2 Sample
This sample modifies the preceding Repeater1 sample so that a page consumer can walk its Items collection during postback to pull out values from it.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI; System.Collections; System.Web.Util
Classes used in this sample:
Repeater; Control; RepeaterItem; Array; ArrayList; TemplateControl; RepeaterItemCollection; Container; EventArgs; Object; Binding; DataBinding; TextBox; String; Page; Assembly
Composition3 Sample
This sample shows a composite control, Composition3
, that raises a custom event, Change
, in response to the TextChanged event of the TextBox child control.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI; System.Web.UI.WebControls
Classes used in this sample:
Control; Button; EventArgs; Literal; TextBox; EventHandler; Object; LiteralControl; WebControl; Assembly; Container; String
Composition2 Sample
This sample shows a composite control, Composition2
, that adds two button controls (named Add
and Subtract
) to the composite control from the previous example and provides event-handling methods for the Click events of the buttons. These methods increment and decrement the Value property of Composition2
.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI; System.Web.UI.WebControls
Classes used in this sample:
Control; Button; Literal; TextBox; LiteralControl; Object; EventHandler; EventArgs; Container; Assembly; WebControl; String
Composition1 Sample
This sample defines a composite control, Composition1
, that combines a System.Web.UI.LiteralControl and a System.Web.UI WebControls.TextBox. Composition1
exposes a custom property, Value
, of type integer, that maps to the Text property of TextBox.
Link to the online sample documentation
Link to the online sample source code
Namespaces used in this sample:
System; System.Web; System.Web.UI; System.Web.UI.WebControls
Classes used in this sample:
Control; TextBox; LiteralControl; EventArgs; Object; Literal; WebControl; Assembly; Container; String