Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 757977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:25:31+00:00 2026-05-14T15:25:31+00:00

I have a few scattered <p> elements on the aspx page which I am

  • 0

I have a few scattered <p> elements on the aspx page which I am grouping together using a class like so – <p class="instructions" runat="server">

In my code behind, using C# I want to hide these elements, using something like
instructions.Visible = false;

However I realize I can only do this in codebehind if I use ID but this will result in invalid HTML/CSS Selector since you can’t have multiple ID’s with the same ID name…

Alternatively is there another way to group the controls if not by class?

EDIT: I can’t use JavaScript, so the selection must be done in C# codebehind/ASP.NET

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:25:31+00:00Added an answer on May 14, 2026 at 3:25 pm

    Aside from grouping all of the controls in a single container control, there is no easy way to find a group of controls given some property in ASP.NET server-side code.

    On the client side, you could use something like jQuery to find these elements and hide them:

    $(".instructions").hide();
    

    I would probably do this in response when the page is fully loaded:

    $(document).ready(function() { 
       $(".instructions").hide(); 
    });
    

    One downside to hiding elements in Javascript is that if there’s enough data it may take a second and cause content to flicker. Another difference is that hiding content client-side does not remove it from the DOM – the content is there just hidden. Hiding controls server-side prevents their content from even being emitted to the HTML.

    Doing the same thing in C# is a bit harder – it requires recursively traversing the control tree and looking for elements in the Control collection that match. This is a common enough operation that a utility function is useful. C# iterator syntax (yield return) is helpful in making this clean:

    // utility method to recursively find controls matching a predicate
    IEnumerable<Control> FindRecursive( Control c, Func<Control,bool> predicate )
    {
        if( predicate( c ) )
            yield return c;
    
        foreach( var child in c.Controls )
        {
            if( predicate( c ) )
                yield return c;
        }
    
        foreach( var child in c.Controls )
            foreach( var match in FindRecursive( c, predicate ) )
               yield return match;
    }
    
    // use the utility method to find matching controls...
    FindRecursive( Page, c => (c is WebControl) && 
                              ((WebControl)c).CssClass == "instructions" );
    

    Hiding the controls now is relatively easy:

    foreach( WebControl c in FindRecursive( Page, c => (c is WebControl) && 
                               ((WebControl)c).CssClass == "instructions" ) )
    {
        c.Visible = false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have few different applications among which I'd like to share a C# enum.
How do I create subdomains in ASP.NET? I have few links on my homepage(Home.aspx)
I have a few C# .dll projects which are common to many applications. Currently,
I have a few lines of PowerShell code that I would like to use
I have few asynchronous tasks running and I need to wait until at least
I'm a newbie to pgsql. I have few questionss on it: 1) I know
We have a few very large Excel workbooks (dozens of tabs, over a MB
I have a few Visual Studio Solutions/Projects that are being worked on in my
I have a few scripts on a site I recently started maintaining. I get
We have a few projects that involve building an application that is composed of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.