Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if a control of a certain type?

Tags:

c#

asp.net

When I use th below code, it works. All the controls are hidden.

foreach (Control ctr in eItem.Controls)
{
    ctr.visible = false;                  
}

However, I want to hide only labels and dropdownlists. That why I'm trying to use the below code without success

foreach (Control ctr in eItem.Controls)
{
    if(ctr is Label | ctr is DropDownList)
    {
       ctr.visible = false;
    }              
}

EDIT

Here's the whole method

 private void HideLabelAndDDLOnPageLoad()
    {
        foreach (ListViewItem eItem in lsvTSEntry.Items)
        {
            foreach (Control ctr in eItem.Controls)
            {
                if (ctr is Label || ctr is DropDownList)
                {
                    ctr.Visible = false;
                }  
            }
        }
    }

When I remove the if, all the controls get hidden. When I put it back, nothing happens.

Thanks for helping

like image 712
Richard77 Avatar asked Aug 03 '12 16:08

Richard77


1 Answers

I think what you are after is || change it to ||...that is the logical or operator.

foreach (Control ctr in eItem.Controls)
{
    if(ctr is Label || ctr is DropDownList)
    {
       ctr.Visible = false;
    }              
}

| = bitwise operator

|| = logical or operator

Based on your edit

It appears your controls are inside an updatepanel, if that is the case you want to loop for all controls within the updatepanel's content template container.

Here you go:

foreach (Control ctr in UpdatePanel1.ContentTemplateContainer.Controls)
 {
  // rest of code
   if(ctr is Label || ctr is DropDownList)
     {
        ctr.Visible = false;
     }         
 }  
like image 89
JonH Avatar answered Sep 30 '22 15:09

JonH