views:

25

answers:

2

I am creating a function to hide all the gridview controls except the control whose name is being passed in the function.

Here this.Controls referes to controls present in the page (At compilation error is thrown although). I want to fetch all the controls of type GridView with Name property not equal to the one passed in the funtion.

Now after getting a collection of GridView. I want to hide all the grids

private void HideGridView(string gridToShow)
{
    GridView[] result = from control in this.Controls
                 where control.GetType() == typeof(GridView) && control.Name !=gridToShow
                 select control as GridView;

    foreach (var control in result)
        control.Visible = false;
}

At comile time it shows me this.Control as invalid because it is not a collection.

How can I achieve this on a web application

EDIT:

private void HideGridView(string gridToShow)
{
    (from control in this.Controls.OfType<GridView>
     where control.Name !=gridToShow
     select control).ToList().ForEach(gv=>gv.Visible=false);
}

Here is the code I wrote, shows me an error: GridView is a type but used like a variable (Compile time error). And at where Invalid Expression Term 'where'

+3  A: 

Where does this method exist? Is it on your page, or in a separate library. If it is a member of your Page-derived class, it shoudl work fine. If you put it in a separate library, you'll need to pass the Page in:

private void HideGridView(Page page, string gridToShow) 
{ 
    (from control in page.Controls.OfType<GridView>()
     where control.Name !=gridToShow 
      select control).ToList().ForEach(gv=>gv.Visible = false);
 }

And it would be called as

  HideGridView(this, "VisibleGrid");

You can also make it an extension method:

static class MyExtensions
{
  public static void HideGridView(this Page page, string gridToShow) 
  { 
       /// same as above.
  }
}

then you could call it as:

  HideGridView("VisibleGrid");
James Curran
....Visible == false);
MUG4N
@MUG4N: No, one equals. I'm setting it, not comparing it.
James Curran
@James: It shows an zigzag error saying GridView is a type but used like a variable.
Shantanu Gupta
Does `Control` have a property `Name`?http://msdn.microsoft.com/en-us/library/system.web.ui.control_members.aspx
hunter
hmmm `GridView` doesn't either....http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview_members.aspx
hunter
@Shantanu: I just fixed a typo, which you may have missed. (Parens at the end of that line)
James Curran
@Hunter: Yes you are right I missed both the things Name to be replaced by ID and () in answer of James.
Shantanu Gupta
+2  A: 

Here you go:

this.Controls.OfType<GridView>().Where(c => c.ID != gridToShow)
    .ToList().ForEach(gv => gv.Visible = false);

And as an extension method:

public static void ShowGridView(this Page page, string gridToShow)
{
    page.Controls.OfType<GridView>().Where(c => c.ID != gridToShow)
        .ToList().ForEach(gv => gv.Visible = false);
}

which will let you call it like this in your Page_Load:

this.Page.ShowGridView("showThisGridView");
hunter