简体   繁体   English

在GridView编辑模式下关闭验证

[英]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. 我有一个页面,该页面的顶部是一个窗体,用于将新记录添加到table1中,而下面是一个GridView,它显示了table1中的记录并具有编辑能力。

Now the form above is simply one text box which allows you to enter names into table1, this textbox has a required field validator. 现在,上面的表单只是一个文本框,允许您在table1中输入名称,此文本框具有必填字段验证器。 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) 验证器会导致问题,但是当gridview处于编辑模式时(如用户按下“更新”时),由于验证器是触发器,因此不会保存任何内容(因为上面的表格中没有任何内容)

How can I fix this? 我怎样才能解决这个问题?

You can set the RequiredFieldValidator Control and the Add button have the same ValidationGroup name. 您可以设置RequiredFieldValidator控件和“添加”按钮具有相同的ValidationGroup名称。 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 您必须在GridView中放入CausesValidation =“ False”

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

Use cause validation to false in the gridview button. 在gridview按钮中使用原因验证为false。

<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 http://www.java2s.com/Code/ASP/Asp-Control/CausesValidationFalse.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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