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

  • SEARCH
  • Home
  • 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.
I have few element in my html page which has tabIndex attribute. Now I
I have few question in this regard When you create an internet page, does
We have few components like libraries dlls When initially created I ran the following
I have few nested DIVs at page. I want to add event only for
I have few Question for which I am not able to get a proper
I have few questions about using lock to protect my shared data structure. I
I have quite a few controls scattered throughout many table cells in my table,
Have few columns and tables, as follows: NOTE : The names of elements used
i have few variables like this: self.lamp_1 self.lamp_2 self.lamp_3 self.lamp_4 and now i want

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.