www.gibmonks.com

Main Page

  Previous Section Next Section

12.4 Methods Reference

DataBind

Page.DataBind( )

Evaluates and resolves any data-binding expressions in the page. It also calls DataBind on all child controls.

Parameters

None.

Example

The following code example uses a data-binding expression to set the ForeColor attribute of a label control tag to the value of local variable named color. When the DataBind method is called in Page_Load, the value of the Color variable is assigned to the ForeColor attribute (which is effectively the same as setting the ForeColor property in code):

<%@ Page Language="vb" %>
<html>
   <head>
      <title></title>
      <script runat="server">
         Dim Color As System.Drawing.Color = System.Drawing.Color.Red
         Sub Page_Load( )
            Message.Text = "ForeColor is: " & Color.Name
            DataBind( )
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" ForeColor="<%# Color %>" runat="server"/>
</body>
</html>

Notes

If you want to perform data binding on a specific control on the page, such as a DataGrid or DataList control, it may be more efficient to call DataBind on that control rather than on the page, since calling it on the control will avoid any overhead in calling DataBind on controls for which data binding is not needed.

FindControl

Control = Page.FinderControl(String)

Returns a reference to the control object whose name corresponds to a search string. The FindControl method is a member of the base Control class.

Parameters

Control

An instance of the Control class that represents the control that is found using the FindControl method. This control must be cast to the correct control type to access members that are specific to the control type.

String

A string containing the programmatic identifier of the control. This value is the same as the ID attribute of a declarative control or, in the case of controls created at runtime, is the same as the object name defined for the control.

Example

The example finds a control using its ID and changes its background color:

Sub Page_Load( )
   Dim TheControl As Control = FindControl("Message")
   If Not TheControl Is Nothing Then
      Dim TheLabel As Label = CType(TheControl, Label)
      TheLabel.Text = "Found the label named Message!"
      TheLabel.BackColor = System.Drawing.Color.Blue
   End If
End Sub

Notes

The FindControl method, which is inherited from the Control class (from which the Page class is derived), is useful when dealing with nested controls or user controls that need to manipulate a control in their parent page. For example, code in a user control could call FindControl on the page containing the user control to locate and manipulate a control contained within the page (but outside the user control).

HasControls

Boolean = Page.HasControls( )

Returns a Boolean value that indicates whether the page contains child controls.

Parameter

Boolean

A Boolean value that indicates whether the page contains child controls.

Example

The code example displays a message indicating whether the page has controls in its Controls collection, based on the value returned by HasControls:

Sub Page_Load( )
   If Page.HasControls = True Then
      Message.Text = "The page contains controls."
   Else
      Message.Text = "The page does not contain controls."
   End If 
End Sub
LoadControl

ObjControl = Page.LoadControl(StrPath)

Returns an instance of the user control defined in the strPath user control file. This allows dynamic loading of user controls instead of using the @ Register directive.

Parameters

objControl

An object of type Control that represents the user control specified in the given path.

strPath

The virtual path to a user control file.

Example

The example uses the LoadControl to load a user control at runtime and adds it to the page's Controls collection:

Sub Page_Load( )
   Dim Hello As UserControl = LoadControl("hello.ascx")
   Page.Controls.Add(Hello)
End Sub

The user control hello.ascx is as follows:

<h1>Hello, World!</h1>
MapPath

String = Page.MapPath(virtualPath)

Returns the physical path that corresponds to a given virtual path.

Parameters

String

A String containing the physical path that corresponds to virtualPath.

virtualPath

A string containing an absolute or relative virtual path.

Example

The example maps the virtual path of the named page to its physical path:

Sub Page_Load( )
   Message.Text = MapPath("MapPath.aspx")
End Sub

Notes

The Page.MapPath method duplicates the functionality of the Server.MapPath method.

ResolveUrl

String = Path.ResolveUrl(strRelativeUrl)

Returns an absolute URL corresponding to a relative URL.

Parameters

String

A string containing the absolute URL.

strRelativeUrl

A relative URL.

Example

The example maps the current relative URL to an absolute URL:

Sub Page_Load( )
   Message.Text = Page.ResolveUrl("ResolveUrl.aspx")
End Sub
Validate

Page.Validate( )

Invokes the validation logic for each validator control on the page. When this method is invoked, it iterates the Page object's ValidatorCollection collection and executes the validation logic associated with each validator control.

Example

See the example for the IsValid property.

Notes

The Validate method is called automatically when the user clicks any HTML or ASP button control whose CausesValidation property is True.

      Previous Section Next Section