Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey Guys!

Thanks for the help of previous posts by the way!

I've created a page for employee assignment (equipment & software)

I have a checkbox list that's connected through the wizard to one of my database tables (DeviceType) which brings up Laptop,Phone,Monitor e.t.c.

I want to place labels on the right hand side. But hide them until a box is ticked.

http://i1145.photobucket.com/albums/o506/MConnolly1991/Capture-4.png?t=1345020789[^]

^^ Is an image of my check box and 2 labels, as you can see. Both say monitor. I'f I check Laptop though, they stay as Monitor, how do I change the second one to Laptop, and if I add a third that it'll change to that choice and so on and so forth?

This is my first time to use check boxes and databases with labels so it's tough enough.

Thanks in advance guys!

Mark
Posted
Comments
Reelix 15-Aug-12 5:09am    
Are these individual CheckBox's, or a single CheckBoxList ?
EuphorialXTC 15-Aug-12 5:10am    
At the moment I'm using a checkboxlist, should I use check boxes and assign each one individually to an item?

Is there any reason you're using multiple label boxes?

The same effect can be achieved with a single one.

Simply set the "AutoPostBack" value of the CheckBoxList to "True" in the "Designer" view, and use the following:

C#
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
            string selectedItems = "";
            for (int j = 0; j < CheckBoxList1.Items.Count; j++)
            {
                if (CheckBoxList1.Items[j].Selected)
                {
                    if (selectedItems.Length == 0)
                    {
                        selectedItems = CheckBoxList1.Items[j].Text;
                    }
                    else
                    {
                        selectedItems += ", " + CheckBoxList1.Items[j].Text;
                    }
                }
            }
            Label1.Text = selectedItems;
        }
 
Share this answer
 
Comments
EuphorialXTC 15-Aug-12 5:24am    
This works great thank you!
The reason I was using many was because I hadn't a clue how to go about doing it.

Thanks very much for your help!

This will be placed into a database and linked to practice engine so hopefully this'll work out in the long run!

Thanks again!
Hi,

You can do it in both Client Side & Server Side.

Client Side using JavaScript
C#
<asp:checkbox id="CheckBoxID" runat="server" onclick="CheckBoxChecked(this)" xmlns:asp="#unknown" />

C#
function CheckBoxChecked(value) {
    if(value.checked == true)
       document.getElementById("<%=LabelId.ClientID %>").style.display = 'none';
    else
       document.getElementById("<%=LabelId.ClientID %>").style.display = 'block' ; 
}

Server Side Event
C#
<asp:checkbox id="CheckBoxID" runat="server" oncheckedchanged="CheckBoxChanged" xmlns:asp="#unknown" />

C#
void CheckBoxChanged(object sender, EventArgs e)  
{  
    LabelID.Visible = false;  
}


Happy Coding :)
 
Share this answer
 

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