2016-02-28 85 views
0

我使用一個GridView分頁顯示數據庫表gridview的插入。當用戶插入UserID時,我想檢查客戶端自定義驗證功能中的重複項。我必須循環通過網格中的所有值,而不僅僅是顯示的值。我無法獲取UserID列中的所有值來檢查是否存在插入的UserID。 這是標記:驗證了重複

<asp:TemplateField HeaderText="UserID"> 
       <HeaderTemplate> UserID 
        <asp:ImageButton ID="senigvUserIDFilter" runat="server" ImageUrl="Images/filter.png" OnClientClick="return ShowHideFilterTxtBox('senigvTxtUserIDFilter')" /> 
        <asp:TextBox ID="senigvTxtUserIDFilter" runat="server" AutoPostBack="true" style="display:none;" ClientIDMode="Static" OnTextChanged="senigvGridFilter_TextChanged"> 
        </asp:TextBox> 
       </HeaderTemplate> 
       <ItemTemplate> 
        <asp:Label ID="senigvLblUserID" runat="server" Text='<%# Bind("UserID") %>' ClientIDMode="Static" 
        ></asp:Label> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:Label ID="senigvLblEditUserID" runat="server" Text='<%# Bind("UserID") %>'></asp:Label>     
       </EditItemTemplate> 
       <FooterTemplate> 
        <asp:TextBox ID="senigvTxtBxInsertUserID" runat="server" Text='<%# Bind("UserID") %>' ClientIDMode="Predictable"></asp:TextBox>  
         <asp:RequiredFieldValidator ID="senigvRequiredFieldInsertUserID" ControlToValidate="senigvTxtBxInsertUserID" runat="server" 
         ErrorMessage="Required field." ValidationGroup="InsertSenderValidation" Display="Dynamic" CssClass="message-error"> 
        </asp:RequiredFieldValidator>    
         <asp:RegularExpressionValidator ID="senigvMaxValInsertUserID" ControlToValidate="senigvTxtBxInsertUserID" runat="server" 
         ErrorMessage="Maximumn length is 40." ValidationGroup="InsertSenderValidation" Display="Dynamic" CssClass="message-error" 
         ValidationExpression="^.{1,40}$" > 
        </asp:RegularExpressionValidator> 
        <asp:CustomValidator ID="senigCustomDupValInsertUserID" runat="server" ControlToValidate="senigvTxtBxInsertUserID" CssClass="message-error" 
         ErrorMessage="*" ClientValidationFunction="ValidateUserID" EnableClientScript="true" 
         ValidationGroup="InsertSenderValidation"> 
        </asp:CustomValidator> 
       </FooterTemplate> 
      </asp:TemplateField> 

這是jQuery的功能, 'ValidateUserID'

function ValidateUserID(sender, args) { 
     args.IsValid = true; 
     sender.innerHTML = ''; 

     var table = document.getElementById("#UserInfoGridVew"); 
     var tbody = table.tBodies[0]; 
     //loop thru all UserIDs 
     for (i = 0, rowLen = tbody.rows.length; i < rowLen; i++) { 
      row = tbody.rows[i]; 
      var currentUserID = row.cells[1].innerHTML; 
      alert('currentUserID: ' + currentUserID); 

      if (args.Value == currentUserID) { 
       //if values are equal; input invalid; set error message 
       args.IsValid = false; 
       sender.innerHTML = "UserID exists."; 
       break; 
      } 
     }  
    }; 

當我使用這個功能,我得到的錯誤Object required上線var tbody = table.tBodies[0];

我需要訪問gridview中的所有數據行,然後檢查UserID列中的重複項。我怎樣才能做到這一點?

回答

0

得到的所有數據,以確定是否是重複的唯一方法是使JSON回調到數據庫和比較值。

網格使用分頁所以要得到所有我不得不做出一個回調到數據庫的值。

0

你能請嘗試以下代碼

function ValidateUserID(sender, args) { 
    args.IsValid = true; 
    sender.innerHTML = ''; 

var gvDrv = document.getElementById("#UserInfoGridVew"); 
for (i = 1; i < gvDrv.rows.length; i++) { 
var cell = gvDrv.rows[i].cells; 
var currentUserID = cell[1].innerHTML; 

alert('currentUserID: ' + currentUserID); 

    if (args.Value == currentUserID) { 
     //if values are equal; input invalid; set error message 
     args.IsValid = false; 
     sender.innerHTML = "UserID exists."; 
     break; 
    } 

} 
} 

打開頁面的查看源代碼,並確保電網的id是「UserInfoGridVew」。

希望這可以幫助你。

+0

我想你的代碼,因爲'table'變量爲空得到一個錯誤。 gridview ID的屬性設置爲'ClientIDMode ='Static''。我檢查了HTML和腳本,id顯示爲'UserInfoGridView'。 –

+0

哎呀,明白了。當我們使用JavaScript時,我們不會在ID前加#。所以它應該只是document.getElementById('UserInfoGridView'); #在jQuery中添加id之前。 –

+0

感謝您的支持。我也應該知道。但'table'變量只返回18行,因爲這是頁面的大小。我需要數據表中的所有行。我嘗試使用'datatable',但變量始終爲空。 –