Changing a control's style based on validation (ASP.NET)
<p>I've got a very simple form with the following troubled snippet:</p>

<pre><code>&lt;asp:Panel class="normal" ID="Panel1" runat="server"&gt;
 &lt;strong&gt;&lt;asp:Label ID="Panel1Error" class="error" Visible="false" runat="server"/&gt;&lt;/strong&gt;

 &lt;label for="TextBox1"&gt;&lt;em&gt;*&lt;/em&gt; Don't leave this blank&lt;/label&gt;
 &lt;asp:TextBox ID="TextBox1" runat="server" /&gt;

 &lt;asp:RequiredFieldValidator ID="TextBox1RFV" runat="server"
  ControlToValidate="TextBox1" ErrorMessage="This field cannot be blank."
  Display="None" /&gt;

    &lt;--- other validators ---&gt;
&lt;/asp:Panel&gt;
</code></pre>

<p>There are two things I want to do when the page fails validation:</p>

<ol>
<li><p>Change the style of Panel1 (to one which shows different colors to indicate an error). I was able to do this by calling Page.Validate in Page_Load, then iterating over Page.Validators, getting each validator's parent control, casting it to a Panel, then setting .CssClass Doesn't seem like a superb solution, but it got the job done - is there a better way?</p></li>
<li><p>I want to take whatever validation error(s) are thrown and put them in the Panel1Error label, as well as set it to visible. This is where I am a bit baffled. I thought at first I could possibly specify the Label in which a validator writes its ErrorMessage, but I had no such luck. If I just toss the validator inside the Label, its formatting messes up the entire layout of the page, regardless of whether I directly assign it the 'error' CSS class or just leave it in the Label.</p></li>
</ol>

<p>Just to clarify, in production, I would be doing this process for multiple Panels on a page, each with one form element, preventing me from calling the Panels explicitly and just saying Panel1.CssClass, etc.</p>
