2012-08-17 31 views
1

我已經將我的用戶控件(my.ascx)放在網頁上(1.aspx)。 my.ascx包含gridview,搜索文本框&搜索按鈕。 我需要根據我在搜索文本框中輸入的文本在搜索按鈕單擊事件中顯示gridview中的行。這是我所做的。使用jQuery進行Gridview過濾用戶控制頁

在1.aspx:

<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script> 
<script type="text/javascript" language="javascript"> 
$(document).ready(function() { 
$('#lblNoRecords').css('display','none'); 

$('#btnSubmit').click(function(e) 
{ 
    // Hide No records to display label. 
    $('#lblNoRecords').css('display','none'); 
    //Hide all the rows. 
    $("#GridView1 tr:has(td)").hide(); 

    var iCounter = 0; 
    //Get the search box value 
    var sSearchTerm = $('#txtSearch').val(); 

    //if nothing is entered then show all the rows. 
    if(sSearchTerm.length == 0) 
    { 
     $("#GridView1 tr:has(td)").show(); 
     return false; 
    } 
    //Iterate through all the td. 
    $("#GridView1 tr:has(td)").children().each(function() 
    { 
     var cellText = $(this).text().toLowerCase(); 
     //Check if data matches 
     if(cellText.indexOf(sSearchTerm.toLowerCase()) >= 0) 
     {  
      $(this).parent().show(); 
      iCounter++; 
      return true; 
     } 
    }); 
    if(iCounter == 0) 
    { 
     $('#lblNoRecords').css('display',''); 
    } 
    e.preventDefault(); 
}) 
}) 
</script> 

在my.ascx:

<asp:TextBox ID="txtSearch" runat="server" ClientIDMode="Static" 
       meta:resourcekey="txtSearchResource1"></asp:TextBox> 

<asp:Button ID="btnSubmit" runat="server" ClientIDMode="Static" CssClass ="ButtonNormal" Text="Search" 
       meta:resourcekey="btnSubmitResource1" /> 

<asp:Label ID="lblNoRecords" runat="server" ClientIDMode="Static" 
       meta:resourcekey="lblNoRecordsResource1"></asp:Label> 
<gridview .......... 
...... 

但沒有任何反應,當我打btnSubmit按鈕。請幫忙。

回答

0

嘗試從更換jQuery選擇的情況下,爲你的GridView ID:

$("#GridView1 tr:has(td)") 

$("#<%= GridView1.ClientID %> tr:has(td)")