How to add an extra CSS class to the surrounding DIV to validate an invalid form?

Here is 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 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!

+3
source share
4 answers

You can replace the div with the ASP panel, runat = server, of course.

<asp:Panel runat="server" ID="Panel1">
    <label for="address1">Address line 1</label>
    <input type="text id="address1" name="address1" class="inputText" />
</asp:Panel>

In the code you can do -

address1_validate.Validate();
...
...
if(address1_validate.IsValid)
     Panel1.CssClass = "required";
else
     Panel1.CssClass = "required error";
+2
source

runat = "server" .

+1

runat = server id div.

From here you have several options. You can override the validation method on the page and change the class settings on this div.

Or you can create a special validator that inherits from the identifier you are looking for. Add a property to hold the div identifier, and as part of the verification code, enter your class as needed.

0
source

Just thought that I would add to this, if you hook OnPreRender to RequiredFieldValidator or CustomValidator and add a class to the checked item / control, you can do it like this:

protected void uxValidator_PreRender(object sender, EventArgs e)
    {
        RequiredFieldValidator uxValidator = sender as RequiredFieldValidator;
        bool isValid = true;
        WebControl control = null;
        if (uxValidator != null)
        {
            isValid = uxValidator.IsValid;
            control = uxValidator.NamingContainer.FindControl(uxValidator.ControlToValidate) as WebControl;
        }
        else
        {
            CustomValidator uxCustomValidator = sender as CustomValidator;
            if (uxCustomValidator != null)
            {
                isValid = uxCustomValidator.IsValid;
                control = uxCustomValidator.NamingContainer.FindControl(uxCustomValidator.ControlToValidate) as WebControl;
            }
        }
        if (control != null)
        {
            if (!isValid)
            {
                control.Attributes["class"] = "error";
            }
            else
            {
                if ((control as TextBox) != null && !string.IsNullOrEmpty((control as TextBox).Text))
                {
                    control.Attributes["class"] = "";
                }
            }
        }
    } 
0
source

Source: https://habr.com/ru/post/1708433/


All Articles