2012-08-17 27 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)")