Victor Charles
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
Thanks,
Victor
Show us your markup
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="A bsolute"
SelectionMode="Multiple"
Text="Select " ForceSelectionText="True" IsEditable="False"
OpenDropDownOnLoad="True" DropDownHeight="130" >
<ItemsTemplate><asp:CheckB ox ID="CheckBox1" runat="server" /> <%# DataBinder.Eval(Container. DataItem, "Receiver")%></ItemsTempla te>
</cc1:C1ComboBox>
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="A
SelectionMode="Multiple"
Text="Select " ForceSelectionText="True" IsEditable="False"
OpenDropDownOnLoad="True" DropDownHeight="130" >
<ItemsTemplate><asp:CheckB
</cc1:C1ComboBox>
Add an OnCheckedChanged event handler to the checkbox.
ASKER
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
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
You can add an attribute to the checkbox and then retrieve its value in your event handler
ASKER
Can you please show me an example on how to do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.findc ontrol(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
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.findc
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
ASKER
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.C1ComboB ox.C1Combo BoxItemCol lection.
On line: Dim chkSelect As CheckBox = CType(C1Receiver.Items.fin dcontrol(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.fin dcontrol(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
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.C1ComboB
On line: Dim chkSelect As CheckBox = CType(C1Receiver.Items.fin
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.fin
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
ASKER
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("C1 Receiver")
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
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("C1
For i = 0 To C1Receiver.Items.Count - 1
Dim chkSelect As CheckBox = CType(ctrl.FindControl(i),
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
dim val as string (Str1) = chkSelect.Text
dim val as string = chkSelect.Text
ASKER
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("C1 Receiver")
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
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("C1
For i = 0 To C1Receiver.Items.Count - 1
Dim chkSelect As CheckBox = CType(ctrl.FindControl(i),
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?
ASKER
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.
Handle the SelectedIndexChange event and then loop through the SelectedItems collection
http://helpcentral.componentone.com/nethelp/c1combobox/default.htm?turl=XMLDocuments%2FC1ComboBoxRef%2Fhtml%2FP_C1_Web_UI_Controls_C1ComboBox_C1ComboBox_SelectedItems.htm
http://helpcentral.componentone.com/nethelp/c1combobox/default.htm?turl=XMLDocuments%2FC1ComboBoxRef%2Fhtml%2FP_C1_Web_UI_Controls_C1ComboBox_C1ComboBox_SelectedItems.htm
ASKER
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 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.
ASKER
OK, I'll keep working at it.