12.3 Collections Reference
ControlCollection = Page.Controls | |
Provides
access to the ControlCollection instance associated
with the page, with which you can add or manipulate controls at
runtime.
Parameter
- ControlCollection
-
An object of type ControlCollection containing the controls
associated with the page.
Example
The code example uses the Controls property to display the Count
property of the ControlCollection class instance
associated with the page. It then adds a new Label control to the
collection and displays the updated Count property by using the new
label.
Sub Page_Load( )
Message.Text = "There are currently " & Controls.Count & _
" controls on the page.<br/>"
Dim Message2 As New Label
Controls.AddAt(Controls.Count - 1, Message2)
Message2.Text = "There are now " & Controls.Count & _
" controls on the page."
End Sub
Notes
As with the Session and Trace properties, while you can retrieve a
local reference to the Controls collection associated with the page,
it is more common to access this instance directly through the
Controls property, as shown in the example.
Note that when adding a control to a page that already contains
controls, using the AddAt method of the
ControlCollection class allows more precise
placement of the control when compared to the Add method, which
simply places the control at the end of the collection. In the
example, using the Add method would result in the output from the
added Label control appearing after the page's
closing </html> tag, which is not
well-formed HTML and could cause the page to render incorrectly in
some browsers.
ValidatorCollection = Page.Validators | |
Returns an instance of the ValidatorCollection
class containing all the validator controls contained on the
requested page. We can access each validator control by iterating the
ValidatorCollection collection.
Parameter
- ValidatorCollection
-
An object variable of type ValidatorCollection.
Example
The code example displays a Textbox control with a
RequiredFieldValidator and RegularExpressionValidator control
assigned to it. In Page_Load, the code iterates through the
ValidatorCollection returned by the Validators property and displays
the ID and ErrorMessage property of each validator in the collection:
<%@ Page Language="vb" %>
<html>
<head>
<title></title>
<script runat="server">
Sub Page_Load( )
Dim Validator as BaseValidator
For Each Validator in Validators
Message.Text &= Validator.ID & " error message: "
Message.Text &= Validator.ErrorMessage & "<br/>"
Next
End Sub
</script>
</head>
<body>
<form runat="server">
Phone: <asp:textbox id="phone" runat="server"/>
<asp:requiredfieldvalidator
id="rfvPhone"
controltovalidate="phone"
display="dynamic"
errormessage="Required!"
runat="server"/>
<asp:regularexpressionvalidator
id="revPhone"
controltovalidate="phone"
display="dynamic"
validationexpression="^[2-9]\d{2}-\d{3}-\d{4}$"
errormessage="Enter a phone number in the form xxx-xxx-xxxx"
runat="server"/>
<br/>
<asp:button id="submit" text="Submit" runat="server"/>
</form>
<br/>
<asp:label id="Message" runat="server"/>
</body>
</html>
Notes
Because we are displaying only properties from the validator controls
that are inherited from the BaseValidator control (from which all
validation controls are derived), we don't need to
cast the validator to its specific type before accessing the
properties. If, however, we wanted to display a property that was
specific to the type of validator used (such as the
ValidationExpression property of the
RegularExpressionValidator class), we would need
to cast the control to the correct type. In Visual Basic .NET, this
is done using the CType keyword.
|