4

I have created a custom BoundField class in C#. I have added ASP.NET validators to the TextBox and exposed the TextBox OnTextChanged event in Edit and Insert modes. I am experiencing an annoyance with the validators though.

I have exposed the validators publicly like in this example:

private RequiredFieldValidator _requiredFieldValidator;
public RequiredFieldValidator RequiredFieldValidator
{
    get { return _requiredFieldValidator ?? (_requiredFieldValidator = new RequiredFieldValidator()); }
}

I have used this approach to avoid having to set up countless custom public properties in the control itself. I can now do this...

<cc1:BoundFieldWithTextChangedEvent DataField="size2" HeaderText="Width" SortExpression="size2" ItemStyle-CssClass="numeric" OnTextChanged="size2_OnTextChanged" AutoPostBack="True"
            RequiredFieldValidator-Text="!"
            RequiredFieldValidator-Display="Dynamic" />

This code compiles fine and the validations work properly on the page; but I am seeing squiggles under the "RequiredFieldValidator-Text" and "RequiredFieldValidator-Display" attributes in Visual Studio 2013. The first warning is:

Validation (ASP.Net): Attribute 'RequiredFieldValidator-Text' is not a valid attribute of element 'BoundFieldWithTextChangedEvent'.

The other warnings are for the rest of the "RequiredFieldValidator-" attributes.

What do I need to do my custom control code to make these attributes behave properly with Visual Studio 2013?

EDIT: Thanks to Alex Lebedev's help below, my problem has been solved.

I needed to do two things:

  1. Add [PersistenceMode(PersistenceMode.InnerProperty)] above my public validator properties in my custom control.
  2. Reference the validators as inner elements of my custom control on the web form page.

<cc1:BoundFieldWithTextChangedEvent DataField="size2" HeaderText="Width" SortExpression="size2" ItemStyle-CssClass="numeric" OnTextChanged="size2_OnTextChanged" AutoPostBack="True" ItemStyle-Wrap="false" ValidationGroup="UpdateItem">
    <RequiredFieldValidator Text="!" ErrorMessage="You must specify a width." Display="Dynamic" SetFocusOnError="true" EnableClientScript="true"></RequiredFieldValidator>
    <CompareValidator Text="*" ErrorMessage="Width must be greater than zero." Operator="GreaterThan" ValueToCompare="0" Type="Double" Display="Dynamic" SetFocusOnError="true" EnableClientScript="true"></CompareValidator>
</cc1:BoundFieldWithTextChangedEvent>

2
  • Don't have VS at hand to try, but i suspect that something like RequiredFieldValidator$Text may work Commented Aug 27, 2015 at 15:14
  • Andrei, thank you for the suggestion; but if I change RequiredFieldValidator-Text to RequiredFieldValidator$Text, I receive an "unexpected token" error. Commented Aug 27, 2015 at 15:17

1 Answer 1

1
<cc1:BoundFieldWithTextChangedEvent DataField="size2" HeaderText="Width" SortExpression="size2" ItemStyle-CssClass="numeric" OnTextChanged="size2_OnTextChanged" AutoPostBack="True"
            RequiredFieldValidator-Text="!"
            RequiredFieldValidator-Display="Dynamic" />

Replace it with:

<cc1:BoundFieldWithTextChangedEvent DataField="size2" HeaderText="Width" SortExpression="size2" ItemStyle-CssClass="numeric" OnTextChanged="size2_OnTextChanged" AutoPostBack="True">
        <RequiredFieldValidator Text="!" Display="Dynamic"/>
</cc1:BoundFieldWithTextChangedEvent>
Sign up to request clarification or add additional context in comments.

2 Comments

Oh! We're getting CLOSER! Now I see... Validation (XHTML5): Element 'requiredfieldvalidator' is not supported.
FIXED! Added [PersistenceMode(PersistenceMode.InnerProperty)] above my RequiredFieldValidator property. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.