Search code examples
c#asp.netrequiredfieldvalidator

How to reference RequiredFieldValidator defined in GridView from code-behind


I have a GridView where I define EditTemplate and FooterTemplate.

GridView has RequiredFieldValidator defined for TextBox:

<FooterTemplate>
   <asp:TextBox ID="txtHolidayNameAdd" runat="server" Width="90%"></asp:TextBox>
   <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtHolidayNameAdd" ErrorMessage="Holiday Name is Required" ForeColor="Red">*</asp:RequiredFieldValidator>
</FooterTemplate>

That Validator should fire only when user adds the data and should be disabled during its editing.

I have a validation routine that displays a jQuery dialog if user did not provide required fields:

function WebForm_OnSubmit() {
    if (typeof (ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) {
        $("#errorDisplay").dialog({
            title: "Validation Error",
            modal: true,
            resizable: false,
            width: 250,
            buttons: {
                Close: function () {
                    $(this).dialog('close');
                }
            }
        });
        return false;
    }
    return true;
}

How can I have validator to validate only when adding data and be disabled when editing?


Solution

  • What you can do is working with validation groups. When you are editing you should remove the validation group property from the validator. When you are done with editing you can apply the validation group again on the validator. I would look in this direction.