SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    WEB CONTROL - Create required text box but not a drop down list

    I have the following code for creating a web control. It basically extends a text box and adds a required field validator and an ajax extension. However, when I change the class that it inherits from to a dropdownlist, I get an error.

    Code:
    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    
    Namespace Mindscape
        <DefaultProperty("Text"), ToolboxData("<{0}:tb runat=server></{0}:tb>")> _
        Public Class tb
            Inherits TextBox
    
            Private oRFV As RequiredFieldValidator
            Private oVCE As AjaxControlToolkit.ValidatorCalloutExtender
    
            <Bindable(True), DefaultValue(False)> _
            Property Required() As Boolean
                Get
                    Return CBool(ViewState(Me.ID & "Required"))
                End Get
                Set(ByVal value As Boolean)
                    ViewState(Me.ID & "Required") = value
                End Set
            End Property
    
            <Bindable(True), DefaultValue("*")> _
            Property ErrorMessage() As String
                Get
                    Return ViewState(Me.ID & "ErrorMessage")
                End Get
                Set(ByVal value As String)
                    ViewState(Me.ID & "ErrorMessage") = value
                End Set
            End Property
    
            Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
                Me.BuildControls()
            End Sub
    
            Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
                MyBase.Render(writer)
                writer.Write("&nbsp;")
                oRFV.RenderControl(writer)
                oVCE.RenderControl(writer)
            End Sub
    
            Protected Sub BuildControls()
                ' required field validator
                oRFV = New RequiredFieldValidator
                oRFV.ID = Me.ID & "_oRFV"
                oRFV.ControlToValidate = Me.ID
                oRFV.ErrorMessage = Me.ErrorMessage
                oRFV.Enabled = Me.Required
                oRFV.Display = ValidatorDisplay.None
                Me.Controls.Add(oRFV)
                ' required extender
                oVCE = New AjaxControlToolkit.ValidatorCalloutExtender
                oVCE.ID = Me.ID & "_oVCE"
                oVCE.TargetControlID = oRFV.ID
                Me.Controls.Add(oVCE)
            End Sub
        End Class
    End Namespace
    Code:
    'MindscapeServerControls.Mindscape.tb' does not allow child controls.
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,576
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    You need to inherit from CompositeControl (I think) and then add the textbox and such to that. As the exception notes, TextBoxes cannot have child controls and since you are inheriting from TextBox you are SOL.

    Protip: make your composite control implement ITextControl if you want it to behave like a text box.

  3. #3
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help. The textbox is fine to add child controls, it's when I make it a drop down list that I get that error. The problem with using the Composite Control is that I have to add properties to mimic what I already have in the textbox. It's not so bad with the textbox, but if I have to do it with a drop down list it will be that much more difficult to add list items.
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  4. #4
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow! I would have thought there would be more discussion on this. That others would have looked into extending the basic input controls.

    Basically, I am looking for a text box, drop down list, and radio button list that I can add a few properties to like required. If I go the route of using the composite control, I end up putting a lot development into just trying to get the control to have the same basic functionality of a text box or a drop down list.
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The composite control is the correct way. You should not extend controls which represent html elements which cannot take child elements.

    It's not such a big deal to extend compositecontrol and delegate properties/methods to an embedded textbox.


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
  •