Imran Imran - 1 year ago 172
ASP.NET (C#) Question

Turning off validation in GridView edit mode

I have a page which consists of a form on top to add new records into table1 and below that a GridView which shows records in table1 with the ability to edit.

Now the form above is simply one text box which allows you to enter names into table1, this textbox has a required field validator. The validator causes issues however when the gridview is in edit mode as when the user presses 'update' nothing is saved because the validator is triggers (as there is nothing in the form above)

How can I fix this?

Answer Source

You can set the RequiredFieldValidator Control and the Add button have the same ValidationGroup name. Something like this:

<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" ValidationGroup="addValidation" />
<asp:RequiredFieldValidator ID="nameRequired" ControlToValidate="txtName" runat="server" ValidationGroup="addValidation"></asp:RequiredFieldValidator>
<asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" />

So that, the Update button won't be impacted.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download