locked
Problem in setting datasource to childcontrols inside compositecontrol RRS feed

  • Question

  • User1825402209 posted

    Hi All,


    I tried to write a composite control to create a dropdown checkboxlist control but having hardtime to get the datasource set to the checkboxlist.

    Even the events are not getting fired, thought I commented it out as I am not able to get the datasource set in the first place.

    Any help would be appreciated.

    Here is my below code:


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

    <ComponentModel.DefaultProperty("DataSource"), _
     ToolboxData("<{0}:CheckBoxDropDownList runat=server></{0}:CheckBoxDropDownList>")> _
    Public Class CheckBoxDropDownList
        Inherits System.Web.UI.WebControls.CompositeControl
        Implements INamingContainer

        Private _dropDownDiv As New System.Web.UI.HtmlControls.HtmlGenericControl("div")
        Private _checkBoxListItems() As ListItem
        Private _selectedCheckBoxListItems As ListItemCollection
        Private _selectedValues As String

        Private _checkBoxList As CheckBoxList
        Private _containerTextBox As TextBox
        Private _containerImage As Image

        Private _popUpDropDown1 As AjaxControlToolkit.PopupControlExtender
        Private _popUpDropDown2 As AjaxControlToolkit.PopupControlExtender

        Private _SelectAllItem As Boolean
        Private _IsMandatory As Boolean
        Private _MandatoryMessage As String

        Public Event CheckBoxDropDownListChanged(ByVal sender As Object, ByVal e As EventArgs)

        Public Property DataTextField()
            Get
                Return Me.ViewState.Item("DataTextField")
            End Get
            Set(ByVal value)
                Me.ViewState.Add("DataTextField", value)
                _checkBoxList.DataTextField = value
            End Set
        End Property

        Public Property DataValueField()
            Get
                Return Me.ViewState.Item("DataTextField")
            End Get
            Set(ByVal value)
                Me.ViewState.Add("DataValueField", value)
                _checkBoxList.DataValueField = value
            End Set
        End Property

        Public Property DataSource()
            Get
                Return ViewState.Item("DataSource")
            End Get
            Set(ByVal value)
                Me.ViewState.Add("DataSource", value)
                _checkBoxList.DataSource = value
            End Set
        End Property

        Public ReadOnly Property SelectedCheckBoxListItems() As ListItemCollection
            Get

                For Each item As ListItem In _checkBoxList.Items
                    If item.Selected Then
                        If _selectedCheckBoxListItems Is Nothing Then
                            _selectedCheckBoxListItems = New ListItemCollection
                        End If
                        _selectedCheckBoxListItems.Add(item)
                    End If
                Next

                Return _selectedCheckBoxListItems
            End Get
        End Property

        Public ReadOnly Property SelectedValues() As String
            Get

                For Each item As ListItem In _checkBoxList.Items
                    If item.Selected Then
                        If _selectedValues Is Nothing Then
                            _selectedValues = New String("")
                        End If
                        _selectedValues += "," & item.Value.ToString
                    End If
                Next

                Return _selectedValues
            End Get
        End Property

        Public Property SelectAllItem() As Boolean
            Get
                Return _SelectAllItem
            End Get
            Set(ByVal value As Boolean)
                _SelectAllItem = value
            End Set
        End Property

        Public Property IsMandatory() As Boolean
            Get
                Return _IsMandatory
            End Get
            Set(ByVal value As Boolean)
                _IsMandatory = value
            End Set
        End Property

        Public Property ErrorMessage() As String
            Get
                Return _MandatoryMessage
            End Get
            Set(ByVal value As String)
                _MandatoryMessage = value
            End Set
        End Property

        Public Sub New()
            MyBase.New()

            _containerTextBox = New TextBox
            _containerImage = New Image
            _checkBoxList = New CheckBoxList
            _popUpDropDown1 = New AjaxControlToolkit.PopupControlExtender
            _popUpDropDown2 = New AjaxControlToolkit.PopupControlExtender
        End Sub


        'Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        '    MyBase.OnInit(e)

        '    AddHandler _checkBoxList.SelectedIndexChanged, AddressOf checkBoxList_SelectedIndexChanged
        'End Sub

        Protected Overloads Overrides Sub CreateChildControls()
            MyBase.CreateChildControls()

            'Set properties of image
            _containerImage.ID = "CDDLImage" & Me.ClientID
            _containerImage.ImageUrl = "http://localhost/test/Images/dropdown.gif"
            '_containerImage.Attributes.Add("OnClick", "toggleDiv" & Me.ClientID & "()")
            _containerImage.Style.Add("padding-left", "1px")
            _containerImage.Style.Add("vertical-align", "middle")
            _containerImage.Width = New Unit(15)
            _containerImage.Height = New Unit(20)

            'Set properties ocf text box
            _containerTextBox.ID = "CDDLImageTextBox" & Me.ClientID
            '_containerTextBox.Attributes.Add("OnClick", "toggleDiv" & Me.ClientID & "()")
            _containerTextBox.Style.Add("padding", "0px 0px 0px 0px")
            _containerTextBox.Style.Add("vertical-align", "middle")
            _containerTextBox.Width = Me.Width.Value * 0.7
            _containerTextBox.Text = Date.Now.ToString
            _containerTextBox.ReadOnly = True

            'Add check box list items to checkboxlist
            _checkBoxList.ID = "CDDLCB" & Me.ClientID
            _checkBoxList.Width = _containerTextBox.Width.Value
            '_checkBoxList.AutoPostBack = True


            'Add checkbox list to dropdowndiv
            _dropDownDiv.ID = "CDDLdropDownDiv" & Me.ClientID
            _dropDownDiv.Controls.Add(_checkBoxList)
            _dropDownDiv.Style.Add("width", (_checkBoxList.Width.Value * 1.1).ToString & "px")
            _dropDownDiv.Style.Add("height", IIf(_checkBoxList.Items.Count * 20 > 250, 250, _checkBoxList.Items.Count * 20).ToString & "px")
            _dropDownDiv.Style.Add("overflow", "auto")
            _dropDownDiv.Style.Add("padding", "0px 0px 0px 0px")
            _dropDownDiv.Style.Add("background-color", "White")
            _dropDownDiv.Style.Add("border-right", "#b7caf5 1px solid")
            _dropDownDiv.Style.Add("border-top", "#b7caf5 1px solid")
            _dropDownDiv.Style.Add("border-left", "#b7caf5 1px solid")
            _dropDownDiv.Style.Add("border-bottom", "#b7caf5 1px solid")


            '_checkBoxList.DataSource = Me.DataSource
            '_checkBoxList.DataTextField = Me.DataTextField
            '_checkBoxList.DataValueField = Me.DataValueField
            '_checkBoxList.DataBind()

            _popUpDropDown1.TargetControlID = _containerTextBox.ID
            _popUpDropDown1.PopupControlID = _dropDownDiv.ID
            _popUpDropDown1.Position = AjaxControlToolkit.PopupControlPopupPosition.Bottom

            _popUpDropDown2.TargetControlID = _containerImage.ID
            _popUpDropDown2.PopupControlID = _dropDownDiv.ID
            _popUpDropDown2.Position = AjaxControlToolkit.PopupControlPopupPosition.Bottom
            _popUpDropDown2.OffsetX = -(_containerTextBox.Width.Value + 4)

            Me.Controls.Add(_containerTextBox)
            Me.Controls.Add(_containerImage)
            Me.Controls.Add(_dropDownDiv)
            Me.Controls.Add(_popUpDropDown1)
            Me.Controls.Add(_popUpDropDown2)

            If _IsMandatory Then
                Dim val As New CheckBoxListRequiredFieldValidator
                val.ControlToValidate = _checkBoxList.ID
                val.ErrorMessage = IIf(_MandatoryMessage Is Nothing, "Choose an item", _MandatoryMessage)
                Me.Controls.Add(val)
            End If

        End Sub

        Public Overrides Sub DataBind()
            MyBase.DataBind()

            _checkBoxList.DataBind()
        End Sub

        'Protected Overrides Sub DataBindChildren()


        '    'AddHandler _checkBoxList.SelectedIndexChanged, AddressOf checkBoxList_SelectedIndexChanged

        '    If _checkBoxListItems IsNot Nothing AndAlso _checkBoxListItems.Length > 0 Then _
        '         _checkBoxList.Items.AddRange(_checkBoxListItems)

        '    MyBase.DataBindChildren()

        'End Sub

        'Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        '    MyBase.OnPreRender(e)

        '    _checkBoxList.DataSource = DataSource
        '    _checkBoxList.DataTextField = DataTextField
        '    _checkBoxList.DataValueField = DataValueField
        '    _checkBoxList.DataBind()


        'End Sub



        'Public Sub checkBoxList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        '    RaiseEvent CheckBoxDropDownListChanged(sender, e)

        'End Sub


    End Class

    Thursday, September 10, 2009 6:12 PM

Answers

  • User1825402209 posted

    Looks like I solved the problem, changed the inheritance to CompositeDataBoundControl


    Overridden the PerformSelect, PerformDataBinding, OnDataBinding methods and set the appropriate values and the events are running fine and the data is going OK.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 11, 2009 6:05 AM