Here is my scenario:
<div class="required">
<label for="address1">Address line 1</label>
<input type="text id="address1" name="address1" class="inputText" />
</div>
<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 html area of the validation error, I can show the message with the following code:
<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 cannot do is add an extra class to the surrounding div tag. I thought I could do something like:
<div class="required <%= !address1_validate.isValid ? "error" : "" %>">
It almost doesn't work.
In any case, I do not want to rely on JavaScript to set these values - it should work like "Web 1.0".
Any ideas?
Thanks, John
------- My solution ------- Here the code 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 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!
source
share