
Using Controls | 75
ASP.NETServer
Controls
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
In addition to the precise control of output, another advantage of this technique is
better control over the appearance of the rendered output. Developers can use
attributes or CSS Styles to modify the appearance of the control, as described later
in this chapter in the section “Modifying Control Appearance.”
Programmatic Control Use
While the declarative technique for control creation is generally simpler and more
straightforward, at times you may want or need to create controls dynamically (e.g.,
in response to some user action). In such cases, developers can create controls
programmatically—either in a server-side
<script> block within the Web Forms
page or in its accompanying code-behind class. You can use either HTML server
controls or web controls with this technique.
You define controls programmatically by declaring a variable of the control’s type,
as shown in the following code snippet (Example 5-2 shows a complete page
using this technique):
' Visual Basic .NET
Dim myText As New TextBox( )
// C#
TextBox myText = new TextBox( );
The New keyword (new in C#) creates a new instance of the desired control. Note
that some controls will accept arguments passed into the constructor for the
control. A Literal control, for example, can accept the