DropDownList with CheckBoxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AnaOlinto
    New Member
    • Aug 2009
    • 4

    DropDownList with CheckBoxes

    Hi,

    For the page I'm developing I need a DropDownList with CheckBoxes on it, so that the users can choose more than one option without having to use the Ctrl key (and it's more intuitive). Because my options are being retrieved from the Database, the list of option can be really big, so using a CheckBoxList would use a lot of space.
    Please, can anyone help me with this? I'm almost desperate! I have been search for a couples day, but having problems in everything I tried.
    What I'm trying to do now is override the DropDownList control (as suggested in this forum: http://www.dotnet247.com/247referenc...42/211693.aspx), but I'm havng problems right in the frist line:

    Public Class CheckBoxDropDow nList : System.Web.UI.WebCont rols.WebControl , INamingContaine r

    System is highlighted and asking me for a declaration ("Declaratio n expected"). What is wrong then? I created this class in the folder App_Code. Is it correct? Please, I need some help with this.

    I'm using ASP.NET 3.5, Visual Studio 2008 and VB.NET. And the DropDownList is inside a GridView.

    Thanks in advance,

    Ana
    Last edited by AnaOlinto; Aug 12 '09, 07:26 PM. Reason: Add more details
  • AnaOlinto
    New Member
    • Aug 2009
    • 4

    #2
    The problem above was because I was using C# syntax. I correct the syntax and the DDL is working. But the thing is: the ChckBoxes are not in the DDL and I don't know ho to get the selectedValues (if more than one). Can anyone help me?
    My code now is like this:

    Code:
    Public Class CheckBoxDropDownList
        Inherits System.Web.UI.WebControls.WebControl
        Implements INamingContainer
    
        Protected Overrides Sub CreateChildControls()
            MyBase.CreateChildControls()
            Dim ddl As DropDownList = New DropDownList()
            Dim li As ListItem = New ListItem("")
            
            ddl.Items.Add(li)
            ddl.Width = New Unit(100)
            ddl.Attributes.Add("multiple", "true")
            ddl.Attributes.Add("onmousedown", "showdiv()")
            ddl.Attributes.Add("onclick", "showdiv()")
            ddl.Attributes.Add("ondragover", "hidediv()")
            ddl.Attributes.Add("onmouseout", "hidediv()")
    
            Dim cbl As CheckBoxList = New CheckBoxList()
            cbl.Width = New Unit(80)
    
            Dim li1 As ListItem = New ListItem("ListItem1")
            Dim li2 As ListItem = New ListItem("ListItem2")
            Dim li3 As ListItem = New ListItem("ListItem3")
    
            cbl.Items.Add(li1)
            cbl.Items.Add(li2)
            cbl.Items.Add(li3)
    
            Dim div As System.Web.UI.HtmlControls.HtmlGenericControl = New System.Web.UI.HtmlControls.HtmlGenericControl("div")
    
            div.ID = "serverdiv"
            div.Controls.Add(cbl)
            div.Style.Add("BORDER-RIGHT", "black 1px solid")
            div.Style.Add("BORDER-TOP", "black 1px solid")
            div.Style.Add("BORDER-LEFT", "black 1px solid")
            div.Style.Add("BORDER-BOTTOM", "black 1px solid")
            div.Style.Add("VISIBILITY", "hidden")
    
            Me.Controls.Add(ddl)
            Me.Controls.Add(div)
        End Sub
    End Class

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      You can use CSS to add a vertical scroll bar to the ChecKBoxList.

      Place the CheckBoxList in a Panel or a <div>. Set the style for this Panel/<div> so that it is the height that you want it to be and also set the style to have overflow:scroll (this will make the scroll bar visible).

      You could also look into using the AjaxToolkit's DropDown control.

      Comment

      Working...