Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>On the client side, you could use something like <a href="http://jquery.com/" rel="noreferrer">jQuery</a> to find these elements and hide them:</p> <pre><code>$(".instructions").hide(); </code></pre> <p>I would probably do this in response when the page is fully loaded:</p> <pre><code>$(document).ready(function() { $(".instructions").hide(); }); </code></pre> <p>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 <em>does not</em> 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.</p> <p>Doing the same thing in C# is a bit harder - it requires recursively traversing the control tree and looking for elements in the <code>Control</code> collection that match. This is a common enough operation that a utility function is useful. <a href="http://msdn.microsoft.com/en-us/library/dscyy5s0(VS.80).aspx" rel="noreferrer">C# iterator syntax</a> (yield return) is helpful in making this clean:</p> <pre><code>// utility method to recursively find controls matching a predicate IEnumerable&lt;Control&gt; FindRecursive( Control c, Func&lt;Control,bool&gt; 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 =&gt; (c is WebControl) &amp;&amp; ((WebControl)c).CssClass == "instructions" ); </code></pre> <p>Hiding the controls now is relatively easy:</p> <pre><code>foreach( WebControl c in FindRecursive( Page, c =&gt; (c is WebControl) &amp;&amp; ((WebControl)c).CssClass == "instructions" ) ) { c.Visible = false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload