![]() ![]() | ||
To test a Web user control, you need a Web application. Typically, you use a Web application that's part of the same solution as the Web user control itself, as we did in the WebUserControls example in the In Depth section of this chapter.
To add a Web user control to a form in the test Web application, you can drag the ASCX file that supports the Web user control onto the form. For example, dragging the file WebUserControl1.ascx file creates the Web user control WebUserControl11. You should also declare that control in the Web form you're using it in, like this:
Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Protected WithEvents WebUserControl11 As WebUserControl1 ⋮
In the WebUserControls example discussed in the In Depth section of this chapter, we tested the Web user control's DisplayColor property, SetText method, and TextModified event in the test application. Here's how that was done in that example's WebForm1.aspx.vb file:
Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Protected WithEvents WebUserControl11 As WebUserControl1 ' Web Form Designer Generated Code... Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load WebUserControl11.DisplayColor = Color.Aqua End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click WebUserControl11.SetText("Hello!") End Sub Private Sub WebUserControl11_TextModified(ByVal NewText As String) _ Handles WebUserControl11.TextModified TextBox1.Text = "New text: " & NewText End Sub End Class
For more on this example and on how we set up the test Web application, see the In Depth section of this chapter.
![]() ![]() | ||