Check box selection in asp.net

Posted by Suneel161 under ASP.NET on 12/3/2010 | Points: 10 | Views : 1582 | Status : [Member] | Replies : 4
i have ten checkboxes and a dropdown list.
when i select 2 items in drop down only two checkboxes should be checked not more than that




Responses

Posted by: T.saravanan on: 12/3/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi Suneel161,

Are you shown same value in CheckBox and DropDownlist? Can you rewrite your question briefly?

Thanks,
T.Saravanan

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suneel161 on: 12/3/2010 [Member] Starter | Points: 25

Up
0
Down
suppose i have ten checkboxes .
and i have dropdown list . in this i have values say 1 to 5.
when i select value 1 only 1 check box should ckecked any where.
just like book my show seat.
if i select 4 ddl it should check only 4 out of ten checkboxes not more than that

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhuk on: 12/3/2010 [Member] Starter | Points: 25

Up
0
Down
If the checkboxes are static then check the sample
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{
if (DropDownList1.SelectedItem.Text == "1")
{
CheckBox1.Checked = true;
CheckBox2.Checked = false;
CheckBox3.Checked = false;
}
else if (DropDownList1.SelectedItem.Text == "2")
{
CheckBox1.Checked = true;
CheckBox2.Checked = true;
CheckBox3.Checked = false;
}
else if (DropDownList1.SelectedItem.Text == "3")
{
CheckBox1.Checked = true;
CheckBox2.Checked = true;
CheckBox3.Checked = true;
}
}


Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 12/3/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi Suneel161,

Sorry for the delay....

Try the following code...

In your DropDownList SelectedIndexChanged Event..

if you using CheckBoxList Control means...

Here ddlList --> DropDownList
chkList --> CheckBoxList

int nCount = int.Parse(ddlList.SelectedItem.ToString());
for (int ix = 0; ix < chkList.Items.Count; ix++)
{
if(ix < nCount)
{
chkList.Items[ix].Selected = true;
}
else
{
chkList.Items[ix].Selected = false;
}
}


if you using CheckBox controls in your page means with out any panel (or) other control....


int nCount = int.Parse(ddlList.SelectedItem.ToString());
int nStart = 1;
foreach(Control cnt in Page.Controls)
{
foreach(Control chcnt in cnt.Controls)
{
if(chcnt is CheckBox)
{
CheckBox chk =(CheckBox)chcnt;
chk.Checked = false;
if(nStart <= nCount)
{
chk.Checked = true;
nStart++;
}
}
}
}


Try the above coding.. If you struck means ask your doubts i will explain...

Cheers :)


Thanks,
T.Saravanan

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response