locked
problem with making a dropdownlist visible in function of the selectedvalue of another dropdownlist RRS feed

  • Question

  • User-1008348326 posted

    Hi,

    i generate 3 dropdownlists. I have to do that with code-behind because i never know in advance how many i need. The amount comes from a database.
    Dropdownlist 3 is not visible, the others are visible.
    When the selectedvalue of dropdownlist 1 equals "b" then dropdownlist 3 must be visible.

    I can do everything except: setting DDL3 visible when selectedvalue of DDL1 equals "b".

    Here the code:

    Friend dds As New List(Of DropDownList)

        Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
            Dim i As Integer
            Dim lit As LiteralControl
            Dim z1, z2, z3 As ListItem
            For i = 1 To 3
                If Not IsPostBack Then
                    Dim dd As New DropDownList
                    dd.AutoPostBack = True
                    dd.ID = i
                    z1 = New ListItem("a", "a")
                    z2 = New ListItem("b", "b")
                    z3 = New ListItem("c", "c")
                    dd.Items.Add(z1)
                    dd.Items.Add(z2)
                    dd.Items.Add(z3)
                    Session(dd.ID) = dd.ID
                    If i = 3 Then dd.Visible = False
                    dds.Add(dd)
                    Session("dds") = dds
                Else
                    dds = CType(Session("dds"), List(Of DropDownList))
                End If
            Next

            For Each d As DropDownList In dds
                form1.Controls.Add(d)
                lit = New LiteralControl("<br>")
                form1.Controls.Add(lit)
                AddHandler d.SelectedIndexChanged, AddressOf dropd
            Next
        End Sub

        Protected Sub dropd(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim dd As DropDownList = CType(sender, DropDownList)
            If dd.ID = 1 And dd.SelectedValue = "b" Then
                'dd with ID=3 must be visible: how to do that?
            End If
        End Sub
    End Class

    Thanks

    Tartuffe

    Monday, May 19, 2008 2:37 AM

Answers

  • User80740001 posted

      'dd with ID=3 must be visible: how to do that?

    Since you named your drop down lists as 1,2 and 3 according to i please try this instead:

    CType(FindControl("1"), DropDownList).Visible = True

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 19, 2008 5:21 AM

All replies

  • User80740001 posted

      'dd with ID=3 must be visible: how to do that?

    Since you named your drop down lists as 1,2 and 3 according to i please try this instead:

    CType(FindControl("1"), DropDownList).Visible = True

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 19, 2008 5:21 AM
  • User-1008348326 posted

    Hi, thanks for replying.

    It works.

    Monday, May 19, 2008 11:01 AM