Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dropdownlist with 30 menus in it .I have to check or uncheck a checkbox as per the selections from dropdown.

Presently i am doing in below approach :
VB
If (ddlbselect.selectedvalue == "Option 1")
Chktest.checked = True
Please let me know if we have any alternate approach for doing the same , i want to avoid this hardcoding as the list is long.
Posted
Updated 8-Aug-13 4:45am
v2

1 solution

Lots of ways of doing this, depending on how you set things up. You're likely going to want to take advantage of the fact that you can store two pieces of information in your dropdownlist items. Here's one way:

VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dt As New DataTable
        dt.Columns.Add("Text")
        dt.Columns.Add("Data")
        Dim dr As DataRow = dt.NewRow
        dt.Rows.Add(New Object() {Nothing, Nothing})
        dt.Rows.Add(New Object() {"odd", "Checkbox1~Checkbox3~Checkbox5"})
        dt.Rows.Add(New Object() {"even", "Checkbox2~Checkbox4"})

        DropDownList1.DataValueField = "Data"
        DropDownList1.DataTextField = "Text"
        DropDownList1.DataSource = dt
        DropDownList1.DataBind()
    End If
End Sub

Private Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    If DropDownList1.SelectedValue.ToString.Length > 0 Then
        Dim chks() As String = DropDownList1.SelectedValue.ToString.Split("~")
        For i As Integer = 0 To chks.Length - 1
            DirectCast(Me.FindControl(chks(i)), CheckBox).Checked = True
        Next
    Else
        For i As Integer = 1 To 5
            DirectCast(Me.FindControl("Checkbox" & i), CheckBox).Checked = False
        Next
    End If

End Sub
 
Share this answer
 
Comments
Ric_Net 12-Aug-13 6:28am    
I have a single checkbox, not multiple... my concern is , i do not want to write multiple times as
If (ddlbselect.selectedvalue == "Option 1")
Chktest.checked = True

If(ddlbselect.selectedvalue == "Option 2")
chkTest.checked = True or false.

In this case if in future , there will be addition to the list , we need to extend the code as above .This is a kind of hardcoding .Please let me knw if i am clear
woopsydoozy 12-Aug-13 10:59am    
Still not clear. "If option 2, then true or false." Well, which is is it? What decides that? If there's any complex logic, you have to write it, and if the logic is different for each dropdown selection, you're not going to have much option but to write it in your code. If, however, you're not using your dropdown value for anything, then you could put your boolean there, and use that to set the Checked property of your checkbox.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900