简体   繁体   中英

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?

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.

You have to put CausesValidation="False" in GridView

<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CausesValidation="False"/>

Use cause validation to false in the gridview button.

<asp:Button ID="Button2" runat="server" 
     CausesValidation="False" 
     Text="Cancel - Will Not Validate!" />

Here is an example:

http://www.java2s.com/Code/ASP/Asp-Control/CausesValidationFalse.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM