[ Team LiB ] |
12.4 Methods Reference
Evaluates and resolves any data-binding expressions in the page. It also calls DataBind on all child controls. ParametersNone. ExampleThe 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> NotesIf 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.
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
ExampleThe 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 NotesThe 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).
Returns a Boolean value that indicates whether the page contains child controls. Parameter
ExampleThe 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
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
ExampleThe 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>
Returns the physical path that corresponds to a given virtual path. Parameters
ExampleThe example maps the virtual path of the named page to its physical path: Sub Page_Load( ) Message.Text = MapPath("MapPath.aspx") End Sub NotesThe Page.MapPath method duplicates the functionality of the Server.MapPath method.
Returns an absolute URL corresponding to a relative URL. Parameters
ExampleThe 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. ExampleSee the example for the IsValid property. NotesThe Validate method is called automatically when the user clicks any HTML or ASP button control whose CausesValidation property is True. |
[ Team LiB ] |