【发布时间】:2016-06-13 10:50:02
【问题描述】:
我有一个 asp.net Listview,并且在 EditItemTemplate 中为编辑操作添加了一个RequiredFieldValidator。
当我单击“编辑”按钮并尝试使用空的 CustomerNameTextBox 保存数据时,出现错误“请输入您的姓名!”
没关系
<%@ Page Language="C#" UnobtrusiveValidationMode="None" AutoEventWireup="true" CodeBehind="frmMain.aspx.cs" Inherits="DB_mit_GridView.frmMain" %>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="CustomerIDLabel1" runat="server" Text='<%# Eval("CustomerID") %>' />
</td>
<td>
<asp:TextBox ID="CustomerNameTextBox" runat="server" Text='<%# Bind("CustomerName") %>' />
<%-- CustomerNameTextBox must not be empty when editing an existing record
<asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="CustomerNameTextBox" errormessage="Please enter your name!" />
</td>
但我不仅要在编辑现有记录时检查空 CustomerNameTextBox,还要在插入新记录时检查。
所以我在 InsertItemTemplate 中为 Insert 操作添加了 RequiredFieldValidator :
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<asp:TextBox ID="CustomerIDTextBox" runat="server" Text='<%# Bind("CustomerID") %>' />
</td>
<td>
<asp:TextBox ID="CustomerNameTextBox" runat="server" Text='<%# Bind("CustomerName") %>' />
<%-- CustomerNameTextBox must not be empty when insertig a new record
<asp:RequiredFieldValidator runat="server" id="reqName2" controltovalidate="CustomerNameTextBox" errormessage="Please enter your name!" />
</td
但只要我将RequiredFieldValidator 添加到InsertItemTemplate,我就会收到消息“请输入您的姓名!”在该页面开始后立即。
我没有点击“插入”按钮 - 消息只是出现,没有任何点击。
那么如何确保某些文本框在编辑或插入时不为空? (我后面没有代码)
【问题讨论】:
标签: asp.net listview requiredfieldvalidator