Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

How to set variables when clicking (Multiselect) on checkboxes of DropdownList on ASP.NET form?

Hello, I have a DropdownList with 5 rows and checkboxes, depending on the rows I check I want to set a value to 5 different variables, for example if row1 is checked x = 1 otherwise x = 0, if row2 is checked y = 1 otherwise y = 0, if row3 is checked z = 1 otherwise z = 0 etc... I also need to automatically check the checkbox when the row/checkbox is clicked on.How do I achieve this? I am suing VB.NET code.

Thanks,

Victor
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Show us your markup
Avatar of Victor  Charles

ASKER

Hello,

Below is the code in my aspx file that builds the control with the checkboxes. As mentioned, I need to set values to different string variables for each checkbox checked but I don't know how to write the code the achieve this.

 <cc1:C1ComboBox ID="C1Receiver" runat="server" Width="180px"
                         Height="30px" BackColor="#CCFFCC" DropDownPositioningMode="Absolute"
                         SelectionMode="Multiple"
                         Text="Select " ForceSelectionText="True" IsEditable="False"
                         OpenDropDownOnLoad="True" DropDownHeight="130" >
                <ItemsTemplate><asp:CheckBox ID="CheckBox1" runat="server" /> <%# DataBinder.Eval(Container.DataItem, "Receiver")%></ItemsTemplate>
                    </cc1:C1ComboBox>
Add an OnCheckedChanged event handler to the checkbox.
What do I include for the OnCheckedChanged event? I need to be able to identify to data in the row I check and order to assign the varaiable a specific value.

Thanks,

Victor
Give it name of your event handler.
You can add an attribute to the checkbox and then retrieve its value in your event handler
Can you please show me an example on how to do this?
ASKER CERTIFIED SOLUTION
Avatar of SAMIR BHOGAYTA
SAMIR BHOGAYTA
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, on line, do you mean replace (As u require) with the name of the string variables?
My combobox control will have 5 rows with checkboxes.

For example taking account only the first two rows. Should the code be:
for i =0 to combobox.items.count -1
        dim chkSelect as CheckBox = ctype(combobox.items.findcontrol(i), CheckBox)
        dim val as string (Str1) = chkSelect.Text
       if str1 = "Name" then
       x = 1
       else
       x = o
      end if
      if str1 = "Item" then
       y = 1
       else
       y = o
      end if
Next



Hi again,

I'm trying your approach with the code below, but I'm running into some difficulties.

Error 1:  Fincontrol is not a member of C1Web.UI.Controls.C1ComboBox.C1ComboBoxItemCollection.
On line: Dim chkSelect As CheckBox = CType(C1Receiver.Items.findcontrol(i), CheckBox)

Error 2:  Arry bound can not appear in type specifiers.
On line: dim val as string (Str1) = chkSelect.Text

How do I solve these two errors?

  Dim str1 As String
        Dim x, y As Integer
        For i = 0 To C1Receiver.Items.Count - 1
            Dim chkSelect As CheckBox = CType(C1Receiver.Items.findcontrol(i), CheckBox)
        dim val as string (Str1) = chkSelect.Text
            If str1 = "Name" Then
                x = 1
            Else
                x = 0
            End If
            If str1 = "Item" Then
                y = 1
            Else
                y = 0
            End If
        Next
Hello,

I was able to solve the first error, but still can't figure out how to solve the second error.

 Dim str1 string
        Dim x, y As Integer
        Dim ctrl As Control = MultiView1.FindControl("C1Receiver")
        For i = 0 To C1Receiver.Items.Count - 1
            Dim chkSelect As CheckBox = CType(ctrl.FindControl(i), CheckBox)
        dim val as string (Str1) = chkSelect.Text
            If str1 = "Name" Then
                x = 1
            Else
                x = 0
            End If
            If str1 = "Item" Then
                y = 1
            Else
                y = 0
            End If
        Next
Change

dim val as string (Str1) = chkSelect.Text

dim val as string = chkSelect.Text
Hello,

I included  your suggestion is the code below, but I received the following error:
"Object reference not set to an instance of an object."
Code:
 Dim str1 As String
        Dim x, y As Integer
        Dim ctrl As Control = MultiView1.FindControl("C1Receiver")
        For i = 0 To C1Receiver.Items.Count - 1
            Dim chkSelect As CheckBox = CType(ctrl.FindControl(i), CheckBox)
            Dim val As String = chkSelect.Text
            If str1 = "BEL" Then
                x = 1
            Else
                x = 0
            End If
            If str1 = "USA" Then
                y = 1
            Else
                y = 0
            End If
        Next
        MsgBox(x)
        MsgBox(y)

For example the Cotrol displays

BEL
CAN
USA

When I click on the checkboxes next to BEL and USA and want

X = 1 for when BEL was selected

and Y = 1 when USA was slected
What is this combobox control you are using? Is it a third party control?
yes, it's a third party contro from componentone. Can you please help me understand part of your code, I don't understand why we are using Dim val As String = chkSelect.Text, it's not used anywhere else Thanks.
That's not my code.  Have you checked the events of the combobox? May be it provides a cleaner way of handle checkboxes by providing events such as ItemChecked etc.
Hello,

I looked at the sample but it wasn'ty much help, is it possible to send me an example in VB.NET?

Thanks,

Victor
I can't because I don't have that control.
OK, I'll keep working at it.