Professional Web Applications Themes

Custom Composite Control inherited from TextBox - ASP.NET Building Controls

Hi, I'm trying to create my own custom control based on the textbox, but with additional properties to specify whether or not the textbox is a required field, and if its needs to be validated against a regular expression. I'd also like it to change its backcolor and be focused on if it's invalid. Here is my current VB code:- Imports System.ComponentModel Imports System.Web.UI Imports System.Web.UI.WebControls Public Class CustomTextBox Inherits TextBox Private req As RequiredFieldValidator Private reg As RegularExpressionValidator Dim _bgErrorColor As System.Drawing.Color Dim _required As Boolean Dim _regExp As String Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs) req ...

  1. #1

    Default Custom Composite Control inherited from TextBox

    Hi,

    I'm trying to create my own custom control based on the textbox, but
    with additional properties to specify whether or not the textbox is a
    required field, and if its needs to be validated against a regular
    expression. I'd also like it to change its backcolor and be focused on
    if it's invalid.

    Here is my current VB code:-

    Imports System.ComponentModel
    Imports System.Web.UI
    Imports System.Web.UI.WebControls

    Public Class CustomTextBox
    Inherits TextBox

    Private req As RequiredFieldValidator
    Private reg As RegularExpressionValidator
    Dim _bgErrorColor As System.Drawing.Color
    Dim _required As Boolean
    Dim _regExp As String

    Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)

    req = New RequiredFieldValidator
    req.ControlToValidate = Me.ID
    req.ErrorMessage = "Value required for " & Me.ID
    req.Text = "*"

    reg = New RegularExpressionValidator
    reg.ControlToValidate = Me.ID
    reg.ErrorMessage = "Invalid value for " & Me.ID
    reg.Text = "*"
    If Me.RegExp <> "" Then reg.ValidationExpression = Me.RegExp

    Controls.Add(req)
    Controls.Add(reg)

    End Sub

    Protected Overloads Overrides Sub Render(ByVal w As HtmlTextWriter)
    MyBase.Render(w)
    If Me.Required Then req.RenderControl(w)
    If Me.RegExp <> "" Then reg.RenderControl(w)
    End Sub

    <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    [Required]() As Boolean
    Get
    Return _required
    End Get

    Set(ByVal Value As Boolean)
    _required = Value
    End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue("")> Property
    [RegExp]() As String
    Get
    Return _regExp
    End Get

    Set(ByVal Value As String)
    _regExp = Value
    End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue("Red")>
    Property [BgErrorColor]() As System.Drawing.Color
    Get
    Return _bgErrorColor
    End Get

    Set(ByVal Value As System.Drawing.Color)
    _bgErrorColor = Value
    End Set
    End Property

    End Class


    It seems to half work, but my required field validator does not appear
    to the immediate right of my textbox, and if I put a validation summary
    on my page, it shows the error message regardless of whether or not
    i've specified the Required property of my control to true or false.

    Does anyone know how i can get this to work, aswell as adding event
    handlers or methods to set the focus to my control and make its
    background color set to whatever i specify in my BgErrorColor property,
    if the control is invalid?

    Many thanks in advance for any suggestions

    Dan Williams

    dan_williams@newcross-nursing.com Guest

  2. #2

    Default Re: Custom Composite Control inherited from TextBox

    <dan_williamsnewcross-nursing.com> wrote in message
    news:1161694790.418053.237340k70g2000cwa.googlegr oups.com...
    > Hi,
    >
    > I'm trying to create my own custom control based on the textbox, but
    > with additional properties to specify whether or not the textbox is a
    > required field, and if its needs to be validated against a regular
    > expression. I'd also like it to change its backcolor and be focused on
    > if it's invalid.
    I'd suggest that you create a composite control instead of deriving from
    TextBox. Your composite control would include the text box, the validator,
    etc., as well as any tables etc. that you might need for layout.

    John


    John Saunders Guest

  3. #3

    Default Re: Custom Composite Control inherited from TextBox

    com wrote: 
    I am guessing here, but what I think is happening is the the
    MyBase.Render is also rendering the validators.
    intrader Guest

Similar Threads

  1. Composite Custom Control - Textbox Readonly Bug
    By Nick Stansbury in forum ASP.NET Building Controls
    Replies: 3
    Last Post: March 20th, 05:55 PM
  2. Problems in Composite Custom Control
    By Sivaji in forum ASP.NET Building Controls
    Replies: 0
    Last Post: November 4th, 04:31 AM
  3. Web Textbox Custom Control
    By biz via DotNetMonster.com in forum ASP.NET Building Controls
    Replies: 0
    Last Post: August 11th, 01:06 PM
  4. User Controls Inherited from a Custom Control share variables?
    By Yuri Vanzine in forum ASP.NET Building Controls
    Replies: 3
    Last Post: July 2nd, 07:55 PM
  5. Replies: 2
    Last Post: July 2nd, 08:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139