Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a checkboxlist control values and text facebook,twitter,Linkdln and Skype.
I have four textboxes, txtfacebook ... txtSkype
the four textboxes are not visible I want it to be visible only when the user check
the listbox. example if the user checks facebook then I want txtFacebook visible.

What I have tried:

// foreach (ListItem item in CBContacts.Items)
// {
// if (item.Selected)
if (CBContacts.SelectedIndex == 0)
{
txtFacebook.Visible = true;
}
else
{
txtFacebook.Visible = false;
}
if (CBContacts.SelectedIndex == 1)
{
txtTwitter.Visible = true;
}
else
{
txtTwitter.Visible = false;
} if (CBContacts.SelectedIndex == 2)
{
txtLinkedIn.Visible = true;
}
else
{
txtLinkedIn.Visible = false;
} if (CBContacts.SelectedIndex == 3)
{
txtSkype.Visible = true;
}
else
{
txtSkype.Visible = false;
}
// }
Posted
Updated 17-Jul-16 18:09pm

1 solution

try this

C#
protected void CBContacts_SelectedIndexChanged(object sender, EventArgs e)
       {
           txtFacebook.Visible = false;
           txtLinkedIn.Visible = false;
           txtSkype.Visible = false;
           txtTwitter.Visible = false;


           foreach (ListItem item in CBContacts.Items)
           {
               if (item.Selected)
               {
                   switch (item.Text)
                   {
                       case "facebook":
                           txtFacebook.Visible = true;
                           break;
                       case "twitter":
                           txtTwitter.Visible = true;
                           break;
                       case "Linkdln":
                           txtLinkedIn.Visible = true;
                           break;
                       case "Skype":
                           txtSkype.Visible = true;
                           break;
                   }
               }
           }
       }



ASP.NET
<asp:CheckBoxList AutoPostBack="true" ID="CBContacts" runat="server" OnSelectedIndexChanged="CBContacts_SelectedIndexChanged">
           <asp:ListItem Text="facebook" />
           <asp:ListItem Text="twitter" />
           <asp:ListItem Text="Linkdln" />
           <asp:ListItem Text="Skype" />
       </asp:CheckBoxList>
 
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