Richard77
Richard77

Reputation: 21621

how to check if a control of a certain type?

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

Upvotes: 6

Views: 24997

Answers (3)

Cosmin Onea
Cosmin Onea

Reputation: 2738

Without your exact markup we can only guess the solution here.

You must be using another container to wrap your controls inside your ItemTemplate in the ListView, something like a Panel or other containers. When you get the Controls on the list view item you actually get the warping container and not its children(labels, dropdowns etc.) One solution to this is something like:

foreach (ListViewItem item in lsvTSEntry.Items)
{
    item.FindControl("myLabel").Visible = false;
    item.FindControl("myDropdownList").Visible = false;
}

Basically you try to find the controls by id and hide them. Notice there is no error checking there so you could get a NullReferenceException if FindControl returns null.

In case you have nested containers in your ItemTemplate and you want to hide all the labels and dropdowns regardless of where they are you can implement your own recursive FindControl that will look like:

private Control FindControlRecursive(Control rootControl, string controlId)
{
    if (rootControl.ID == controlId)
    {
        return rootControl;
    }

    foreach (Control controlToSearch in rootControl.Controls)
    {
        Control controlToReturn = FindControlRecursive(controlToSearch, controlId);
        if (controlToReturn != null)
        {
            return controlToReturn;
        }
    }

    return null;
}

Not the most elegant but.... You can change this to take an array of Id's of course for speed purposes. Based on this of course you can implement the search by control type which instead of taking a controlId as a parameter will take the types of controls to find.

Upvotes: 1

JonH
JonH

Reputation: 33143

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;
     }         
 }  

Upvotes: 15

Marty
Marty

Reputation: 7522

The | is the bitwise or operator. You are looking for ||, the logical or operator.

 if(ctr is Label || ctr is DropDownList)

Upvotes: 1

Related Questions