Share via


Setting User Control Properties Programmatically at Run Time

User controls have properties that affect how the controls are displayed. These properties can be set at design time or at run time. The following example shows how a control is used at run time. For more information about setting properties at design time, see Setting User Control Properties at Design Time.

In the following example, suppose there is a simple user control "Header" that exposes the property BannerImagePath as a public property. The control uses this property as the path to the image resource in the header.

In this case, the site developer can easily get or set the property value programmatically at run time, as shown in the following code from the (aspx.vb) page:

Public Class DefaultForm
  Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
…
#End Region
  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim header as HeaderCtrl = Page.FindControl( “Header”)
    If HeaderCtrl is nothing Then
      Throw new ControlNotFoundException
    End If
    If DateTime.Now.Hour < 12 Then
      headerCtrl.BannerImagePath = “./goodmorning.jpg”
    Else
      headerCtrl.BannerImagePath = “./goodafternoon.jpg”
    End if
  End Sub
End Class

Copyright © 2005 Microsoft Corporation.
All rights reserved.