Here's my scenario:
<!-- Normal Control -->
<div class="required">
<label for="address1">Address line 1</label>
<input type="text id="address1" name="address1" class="inputText" />
</div>
<!-- Same Control - but with a validation error -->
<div class="required error">
<p class="error">Address Line 1 Field is required</p>
<label for="address1">Address line 1</label>
<input type="text id="address1" name="address1" class="inputText" />
</div>
In the "validation error" html area, I'm able to show the message using code like this:
<div class="required">
<asp:RequiredFieldValidator id="address1_validate" runat="server" ControlToValidate="address1" Text='<p class="error">Address Line 1 Field is required</p>' />
<label for="address1">Address (line 1)</label>
<asp:TextBox id="address1" CssClass="inputText" CausesValidation="true" runat="server"/>
</div>
What I'm not able to do is add the additional class to the surrounding div tag. I was thinking that I could do something like:
<div class="required <%= !address1_validate.isValid ? "error" : "" %>">
That pretty much doesn't work.
Anyway, I don't want to have to rely on JavaScript to set these values - it needs to work like "Web 1.0".
Any ideas?
Thanks, Jon
------- My Solution------- Here's the code behind that worked for me:
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
address1_validate.Validate();
if (!address1_validate.IsValid)
{
address_panel.CssClass = "required error";
}
}
}
And the front-end:
<asp:Panel runat="server" id="address_panel" CssClass="required">
<asp:RequiredFieldValidator id="address1_validate" runat="server" ControlToValidate="address1" Text='<p class="error">Address Field is required</p>' />
<label for="address1">Address (line 1)</label>
<asp:TextBox id="address1" CssClass="inputText" CausesValidation="true" EnableViewState="true" runat="server" />
</asp:Panel>
Thanks for the help!