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:
- Add
[PersistenceMode(PersistenceMode.InnerProperty)]above my public validator properties in my custom control. - 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>
RequiredFieldValidator$Textmay workRequiredFieldValidator-TexttoRequiredFieldValidator$Text, I receive an "unexpected token" error.