Advertisement
UpgradePolecat

Custom Web Control VB.NET

Aug 16th, 2011
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.91 KB | None | 0 0
  1. Imports System.Web.UI
  2. Imports System.Web.UI.WebControls
  3.  
  4. Public Class LabeledCompositeControl
  5.     Inherits CompositeControl
  6.  
  7.     Private _panel As Panel
  8.     Protected ReadOnly Property Panel As Panel
  9.         Get
  10.             If _panel Is Nothing Then
  11.                 _panel = New Panel()
  12.             End If
  13.             Return _panel
  14.         End Get
  15.     End Property
  16.  
  17.     Private _label As Label
  18.     Protected ReadOnly Property Label As Label
  19.         Get
  20.             If _label Is Nothing Then
  21.                 _label = New Label With {
  22.                     .Visible = True
  23.                 }
  24.             End If
  25.             Return _label
  26.         End Get
  27.     End Property
  28.  
  29.     Public Property LabelText As [String]
  30.         Get
  31.             Return Label.Text
  32.         End Get
  33.         Set(value As String)
  34.             Label.Text = value
  35.         End Set
  36.     End Property
  37.  
  38.     ''' <summary>
  39.     ''' Geeft aan of er een linebreak tussen het label en control geplaatst moet worden.
  40.     ''' </summary>
  41.     ''' <value></value>
  42.     ''' <returns></returns>
  43.     ''' <remarks>Default waarde is False</remarks>
  44.     Public Property RenderBreak As Boolean
  45.         Get
  46.             Return (If(ViewState("RenderBreak") Is Nothing, False, CBool(ViewState("RenderBreak"))))
  47.         End Get
  48.         Set(value As Boolean)
  49.             ViewState("RenderBreak") = value
  50.         End Set
  51.     End Property
  52.  
  53.     Protected Overridable Sub SetAssociatedControlID(ByRef AssociatedControlID As String)
  54.     End Sub
  55.  
  56.     Public Overrides ReadOnly Property Controls As ControlCollection
  57.         Get
  58.             Return Panel.Controls
  59.         End Get
  60.     End Property
  61.  
  62.     Protected Overrides Sub CreateChildControls()
  63.         CssClass = "labeledcontrol"
  64.         Label.CssClass = "label"
  65.  
  66.         Dim associatedControlID As String = ""
  67.         SetAssociatedControlID(associatedControlID)
  68.  
  69.         If Not String.IsNullOrEmpty(associatedControlID) Then
  70.             Label.AssociatedControlID = associatedControlID
  71.         End If
  72.  
  73.         Panel.Controls.Add(Label)
  74.  
  75.         If RenderBreak Then
  76.             Panel.Controls.Add(New Literal With {.Text = "<br />"})
  77.         End If
  78.  
  79.         MyBase.Controls.Add(Panel)
  80.     End Sub
  81.  
  82.     ''' <summary>
  83.     ''' Needed for validators trying to find controls outside their namingcontainer
  84.     ''' http://www.michaelkbell.com/index.php/2009/04/21/validation-against-controls-outside-of-the-current-namingcontainer/
  85.     ''' </summary>
  86.     ''' <param name="id"></param>
  87.     ''' <param name="pathOffset"></param>
  88.     ''' <returns></returns>
  89.     Protected Overrides Function FindControl(id As String, pathOffset As Integer) As Control
  90.         ' try to find the control the "normal" way
  91.         Dim c As Control = MyBase.FindControl(id, pathOffset)
  92.         If c IsNot Nothing Then
  93.             Return c
  94.         End If
  95.  
  96.         ' if it wasn't found try the "other" way ...
  97.         Return FindControl(Page, id)
  98.     End Function
  99.  
  100.     ''' <summary>
  101.     ''' Recursively find a control based on its ID and start searching at "my" parent's controls
  102.     ''' </summary>
  103.     ''' <param name="parent"></param>
  104.     ''' <param name="id"></param>
  105.     ''' <returns></returns>
  106.     Public Overloads Function FindControl(parent As Control, id As String) As Control
  107.         Dim recurse As Control
  108.         If parent.ID = id Then
  109.             Return parent
  110.         End If
  111.  
  112.         For Each child As Control In parent.Controls
  113.             recurse = FindControl(child, id)
  114.             If recurse IsNot Nothing Then
  115.                 Return recurse
  116.             End If
  117.         Next
  118.  
  119.         Return Nothing
  120.     End Function
  121. End Class
  122.  
  123. Public Class LabeledTextBox
  124.     Inherits LabeledCompositeControl
  125.  
  126.     Private _textBox As TextBox
  127.  
  128.     Public Property [Mode] As String
  129.         Set(value As String)
  130.             Select Case value
  131.                 Case "MultiLine"
  132.                     TextBox.TextMode = TextBoxMode.MultiLine
  133.                     Exit Select
  134.                 Case Else
  135.                     TextBox.TextMode = TextBoxMode.SingleLine
  136.                     Exit Select
  137.             End Select
  138.         End Set
  139.         Get
  140.             Return TextBox.TextMode.ToString()
  141.         End Get
  142.     End Property
  143.  
  144.     Public Property Rows As Integer
  145.         Set(value As Integer)
  146.             TextBox.Rows = value
  147.         End Set
  148.         Get
  149.             Return TextBox.Rows
  150.         End Get
  151.     End Property
  152.  
  153.     Public Property Columns As Integer
  154.         Set(value As Integer)
  155.             TextBox.Columns = value
  156.         End Set
  157.         Get
  158.             Return TextBox.Columns
  159.         End Get
  160.     End Property
  161.  
  162.     Public Property EnteredText As String
  163.         Set(value As String)
  164.             TextBox.Text = value
  165.         End Set
  166.         Get
  167.             Return TextBox.Text
  168.         End Get
  169.     End Property
  170.  
  171.     Protected Overrides Sub SetAssociatedControlID(ByRef AssociatedControlID As String)
  172.         AssociatedControlID = TextBox.ID
  173.     End Sub
  174.  
  175.     Public ReadOnly Property TextBox As TextBox
  176.         Get
  177.             If _textBox Is Nothing Then
  178.                 _textBox = New TextBox() With {
  179.                     .ID = "txtTextbox",
  180.                     .Visible = True
  181.                 }
  182.             End If
  183.             Return _textBox
  184.         End Get
  185.     End Property
  186.  
  187.     Public Property [ReadOnly] As Boolean
  188.         Set(value As Boolean)
  189.             TextBox.[ReadOnly] = value
  190.         End Set
  191.         Get
  192.             Return TextBox.[ReadOnly]
  193.         End Get
  194.     End Property
  195.  
  196.     Public Property MaxLength As Integer
  197.         Set(value As Integer)
  198.             TextBox.MaxLength = value
  199.         End Set
  200.         Get
  201.             Return TextBox.MaxLength
  202.         End Get
  203.     End Property
  204.  
  205.     Protected Overrides Sub CreateChildControls()
  206.         MyBase.CreateChildControls()
  207.  
  208.         TextBox.Width = Width
  209.         TextBox.CssClass = "textbox"
  210.         Controls.Add(TextBox)
  211.     End Sub
  212. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement