Nightmare Validator in C # Dynamic Management

I have a requirement to add RequiredFieldValidator and RegularExpressionValidator to a dynamically created text box in a dynamically created table cell inside the web user in the content area of ​​the page created by the wizard.

The problem, as you might guess, is trying to dynamically set the ControlToValidate property to view a dynamically created text field.

After some research, the code now:

  • Creates a panel (since I heard that ControlToValidate and Validator must be in the same container). It was originally a placeholder, but he tried to propose the following proposal.
  • Creates a text field and sets its identifier.
  • Adds a text box to the panel.
  • Creates a RequiredFieldValidator.
  • Sets the identifier of the ControlToValidate control. The values ​​I tried to use are:

    • Control id
    • control identifier Client
    • control identifier, prefix of the added text that the server attaches to the child web user controls.
    • Customer ID changed in the same way.
    • control name (if possible)
    • name of the prefix text control that the server adds to the names of the controls
    • using the custom Recursive FindControl method in an attempt to create a new Control object in the text field and then use its identifier and client identifier
    • Unique control
    • same with prefix as described above
  • Add a validator to the panel.
  • Add a panel to the table.

, "" , , .

EDIT: , page_load. , . , , .

?

+3
5

, ? ControlToValidate Visual Studio, , .

+1

:

<table>
<colgroup>
    <col style="white-space: nowrap;" />
    <col />
    <col />
</colgroup>
<asp:Repeater ID="InputFields" runat="server">
    <ItemTemplate>
        <tr>
            <td class="labelCell">
                <asp:Label id="FieldName" runat="server" Font-Bold="True" Text='<%# Eval("Name") %>'></asp:Label>:
            </td>
            <td class="fieldCell">
                <asp:TextBox id="FieldData" runat="server" autocomplete="off" />
            </td>
            <td class="errorCell">
                <asp:RequiredFieldValidator ID="FieldNameRequiredValidator" runat="server" CssClass="errorValidator" ErrorMessage='<%# Eval("Name") %> is required' 
                    ControlToValidate="FieldData" Display="Dynamic">&nbsp;&nbsp;&nbsp;</asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="FieldNameRegexValidator" runat="server" CssClass="errorValidator" ErrorMessage='A valid <%# Eval("Name") %> is required'
                    ControlToValidate="FieldData" Display="Dynamic" ValidationExpression='<%# Eval("RegEx") %>'>&nbsp;&nbsp;&nbsp;</asp:RegularExpressionValidator>
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

" ", , FieldData .

+1

:

= "PanelHolder" ( ).

:

var myTextbox = new TextBox() {ID="myTextBox"};
PanelHolder.Controls.Add(myTextBox);
var validator = new RequiredFieldValidator() {ControlToValidate="myTextBox",Display=ValidatorDisplay.Dynamic,ErrorMessage="Required field"}
PanelHolder.Controls.Add(validator);
0

:

  • , , ? ( , , )

  • , ? ErrorMessage , , . ( , )

Edit:

- :

        Panel pTest = new Panel();

        TextBox tb = new TextBox();
        for (int i = 0; i < 2; i++)
        {
            tb.ID = "tbDynamicTextBox" + i;
            pTest.Controls.Add(tb );
            RequiredFieldValidator rfv = new RequiredFieldValidator();
            rfv.ControlToValidate = tb.ID;
            rfv.ErrorMessage = "Empty textbox";
            pTest.Controls.Add(rfv);
        }
        cell.Controls.Add(pTest);

, . "TextBox tb = new TextBox(); ' tho, .

, , .

0

i texbox .

At first I tried to use the clientbox identifier as the controltovalidate property of the required validation field, this did not allow me to find a control error than I gave the texbox identifier as the controltovalidate validfield validator property, and it worked for me. The following is the RegularExpressionValidator code for the control that is passed as the first argument to the method.

private RegularExpressionValidator GetRegValidator(string itemId, string regExp)
    {
        RegularExpressionValidator _regVal = new RegularExpressionValidator();
        _regVal.ControlToValidate = itemId;
        _regVal.ValidationExpression = regExp;
        _regVal.ErrorMessage ="PropertyRegexDoesNotMatches";
        _regVal.Text = "*";
        _regVal.SetFocusOnError = true;
        _regVal.EnableClientScript = true;
        _regVal.ID = string.Format("{0}Validator", itemId);
        return _regVal;
    }
0
source

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


All Articles