نحوه دسترسی به مقادیر کنترلهای کاربر (ارتباطات پایه) نحوه ارتباط صفحه با کنترل کاربر

   1:  'VB.NET - هدر کنترل کاربر
   2:  Public Class ResultHeader
   3:  Inherits System.Web.UI.UserControl
   4:   Private Const headerTemplate As String = "Page {1} of {2}"
   5:   Protected header As Literal
   6:   Private currentPage As Integer
   7:   Private recordsPerPage As Integer
   8:
   9:   Public Property CurrentPage() As Integer
  10:     Get
  11:       Return currentPage
  12:     End Get
  13:     Set
  14:       currentPage = value
  15:     End Set
  16:   End Property
  17:
  18:   Public Property RecordsPerPage() As Integer
  19:     Get
  20:       Return recordsPerPage
  21:     End Get
  22:     Set
  23:       recordsPerPage = value
  24:     End Set
  25:   End Property
  26:
  27:   Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  28:     header.Text = headerTemplate
  29:     header.Text = header.Text.Replace("{1}", currentPage.ToString())
  30:     header.Text = header.Text.Replace("{2}", recordsPerPage.ToString())
  31:   End Sub
  32:  End Class

از صفحه به کنترل کاربر

    

کد وی بی

   1:  'VB.NET
   2:  dim c as Control = Page.LoadControl("Results.ascx")
   3:  c.Info = SomeBusinessLayer.GetAllResults()
       'not valid, Info isn't a member of Control
   4:
   5:  dim r as Results = ctype(Page.LoadControl("Results.ascx"), Results)
   6:  r.Info = SomeBusinessLayer.GetAllResults() 

از کنترل کاربر به صفحه

   1:  'VB.NET
   2:  Dim pageTitle As String = Nothing
   3:  If TypeOf (Page) Is SamplePage Then
   4:     pageTitle = CType(Page, SamplePage).Title
   5:  Else
   6:     pageTitle = "unknown"
   7:  End If

از کنترل کاربر به کنتر کاربر

کد صفحه:

 

   1:  Public Class SamplePage
   2:   Inherits System.Web.UI.Page
   3:   Private rr As Results
   4:   Private rh As ResultHeader
   5:   Private _title As String
   6:   Public ReadOnly Property Title() As String
   7:    Get
   8:     Return _title
   9:    End Get
  10:   End Property
  11:   Public ReadOnly Property Results() As Results
  12:    Get
  13:     Return rr
  14:    End Get
  15:   End Property
  16:   Public ReadOnly Property Header() As ResultHeader
  17:    Get
  18:     Return rh
  19:    End Get
  20:   End Property
  21:  ...
  22:  End Class

   کد سمت کنترل کاربری

   1:  'VB.NET
   2:  Private Sub Page_Load(ByVal sender As System.Object, _
               ByVal e As System.EventArgs) Handles MyBase.Load
   3:   Dim info As DataTable
   4:   If TypeOf (Page) Is SamplePage Then
   5:    info = CType(Page, SamplePage).Results.Info
   6:   End If
   7:  End Sub