2016-06-13 51 views
0

我有一個asp.net Listview,並且我已經爲EditItemTemplate中的編輯操作添加了一個RequiredFieldValidator。asp.net - Listbox - InsertItemTemplate RequiredFieldValidator

當我點擊「編輯」按鈕,並嘗試用空的CustomerNameTextBox保存數據,然後我得到錯誤「請輸入你的名字!」
這是OK

<%@ 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。
所以我想補充的RequiredFieldValidator的插入操作在InsertItemTemplate元素:

<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以InsertTemplate則我得到的消息「請輸入你的名字!」緊接着頁面開始之後。
我沒有點擊「插入」按鈕 - 消息剛剛出現沒有任何點擊。

那麼在編輯或插入時如何確保某些文本框不爲空? (我沒有後面的代碼)

回答