' WebUserControl_First_Last_Name.vb.txt ' WebUserControl.ascx.vb '==================================================================== ' 1... Create Control name “WebUserControl.ASCX” ' 2... VS.NET generates @Control directive in HTML ' 3... Add controls and functionality ' 4... Expose: ' Properties - ie FullName() As String ' Methods - ie ClearName() ' 5... Drag UserControl onto WebForm (name “UserName”) ' 6... VS.NET generates @Register directive in HTML ' 7... Add: Protected WithEvents UserName As WebUserControl ' 8... Reference Properties and Methods of UserControl ' Sub btnGetUserName_Click(...) Handles btnGetName.Click ' lblUserName.Text = UserName.FullName ' End Sub ' ' Sub btnClear_Click(...) Handles btnClear.Click ' UserName.ClearName() ' End Sub ' '==================================================================== Public Class WebUserControl Inherits System.Web.UI.UserControl ' Region " Web Form Designer Generated Code " Protected WithEvents txtFirst As System.Web.UI.WebControls.TextBox Protected WithEvents txtLast As System.Web.UI.WebControls.TextBox Protected WithEvents lblFirstName As System.Web.UI.WebControls.Label Protected WithEvents lblLastName As System.Web.UI.WebControls.Label Private m_FullName As String Public ReadOnly Property FullName() As String Get If chkTextBox(txtFirst, lblFirstName) And chkTextBox(txtLast, lblLastName) Then m_FullName = txtLast.Text & ", " & txtFirst.Text Else m_FullName = "" End If FullName = m_FullName End Get End Property Private Function chkTextBox(ByVal tb As TextBox, ByVal lb As Label) As Boolean Dim bolOK As Boolean If (tb.Text.Length > 0) Then lb.BackColor = Color.Green bolOK = True Else lb.BackColor = Color.Red bolOK = False End If Return bolOK End Function Public Sub ClearName() m_FullName = "" txtFirst.Text = "" txtLast.Text = "" lblFirstName.BackColor = Color.White lblLastName.BackColor = Color.White End Sub End Class ============================================================================== <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="UserControl.WebForm1"%> <%@ Register TagPrefix="uc1" TagName="WebUserControl" Src="WebUserControl.ascx" %> WebForm1

============================================================================== Public Class WebForm1 Inherits System.Web.UI.Page #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent() End Sub Protected WithEvents btnGetUserName As System.Web.UI.WebControls.Button Protected WithEvents lblUserName As System.Web.UI.WebControls.Label Protected WithEvents UserName As WebUserControl Protected WithEvents btnClear As System.Web.UI.WebControls.Button 'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Init InitializeComponent() End Sub #End Region Private Sub btnGetUserName_Click( ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnGetUserName.Click lblUserName.Text = UserName.FullName End Sub Private Sub btnClear_Click( ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnClear.Click UserName.ClearName() End Sub End Class