i tried like this:
[code]
private void checkboxlist1_SelectedIndexChanged(object sender, EventArgs e)
{
int[] v=new int[6];
for(int i=0;i<6;i++)
if(checkboxlist1.getitemchecked(i))
v[i]=1;
else
v[i]=0;
string w=v.ToString();
}
[/code]
and then , the result is wrong. for example, if i check the first three items, the result will be 110000. it seems it's a step behind or something. should i use itemcheck event instead of selectedindexchanged, or both, or what? thank you
Suthish NairPosted Dec 9, 2010, 1:11 PM
Welcome to C# Corner..
Am not sure in understanding your problem, but give this a try..
Subhendu DePosted Dec 8, 2010, 11:47 PM
I have taken one checkboxlist and button..... I took button bcz onclick of button I can set the 0/1 to array for checked/unchecked checkboxes.
code behind
=========
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int[] _array = new int[6];
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
_array[i] = 1;
}
for (int j = 0; j < _array.Length; j++)
{
Response.Write(_array[j].ToString() + "
");
}
}
}
This is codebehind written in C#.
I took one array of size 6 since my checkboxlist item count is 6. If you take integer array by default, the values are 0 in array items.
Then I am traversing the checkboxlist items and set array location to 1 for the checked checkbox.
These whole things I am doing on button click event rather checkboxlist selectedcchengedEvent.
Hope this help you to understand what I tried. If you have other requirement, then feel free to reply.
Thanks....
Sam HobbsPosted Dec 8, 2010, 9:45 PM
Subhendu DePosted Dec 8, 2010, 4:20 AM
=======
code behind
=========
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int[] _array = new int[6];
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
_array[i] = 1;
}
for (int j = 0; j < _array.Length; j++)
{
Response.Write(_array[j].ToString() + "
");
}
}
}
Is there any need to put the code in selectedindexchanged event?
Thanks