Evaluates and resolves any data-binding expressions in the page. It also calls DataBind on all child controls.
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>
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.
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.
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.
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.
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
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).
Boolean = Page.HasControls( )
Returns a Boolean value that indicates whether the page contains child controls.
A Boolean value that indicates whether the page contains child controls.
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
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.
An object of type Control that represents the user control specified in the given path.
The virtual path to a user control file.
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:
String = Page.MapPath(virtualPath)
Returns the physical path that corresponds to a given virtual path.
A String containing the physical path that corresponds to virtualPath.
A string containing an absolute or relative virtual path.
The example maps the virtual path of the named page to its physical path:
Sub Page_Load( ) Message.Text = MapPath("MapPath.aspx") End Sub
The Page.MapPath method duplicates the functionality of the Server.MapPath method.
String = Path.ResolveUrl(strRelativeUrl)
Returns an absolute URL corresponding to a relative URL.
A string containing the absolute URL.
A relative URL.
The example maps the current relative URL to an absolute URL:
Sub Page_Load( ) Message.Text = Page.ResolveUrl("ResolveUrl.aspx") End Sub
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.
See the example for the IsValid property.
The Validate method is called automatically when the user clicks any HTML or ASP button control whose CausesValidation property is True.