Gridview文本框过滤器 - 分页不工作

时间:2018-02-07 01:09:58

标签: javascript asp.net gridview textbox

我在我的应用程序中工作,使用gridview进行排序和过滤,用户需要一个过滤器来使用更具体的过滤器,我使用的是javascript过滤器并且它可以工作,但是当gridview有2页或更多时,它只过滤第一个。

这是javascript过滤器:

<!-- Gridview Filtro -->
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
    $('#<%=lblNoRecords.ClientID%>').css('display','none');

    $('#<%=Button1.ClientID%>').click(function(e)
    {
        $('#<%=lblNoRecords.ClientID%>').css('display','none'); 
        $("#<%=gvwSacBus_Docs.ClientID%> tr:has(td)").hide(); // Hide rows.

        var iCounter = 0;
        var sSearchTerm = $('#<%=TextBox1.ClientID%>').val(); //search value

        if(sSearchTerm.length == 0) //all rows if textbox empty
        {
          $("#<%=gvwSacBus_Docs.ClientID%> tr:has(td)").show(); 
          return false;
        }
        //Iterate 
        $("#<%=gvwSacBus_Docs.ClientID%> tr:has(td)").children().each(function() 
        {
            var cellText = $(this).text().toLowerCase();
            if(cellText.indexOf(sSearchTerm.toLowerCase()) >= 0) //Check matches
            {    
                $(this).parent().show();
                iCounter++;
                return true;
            } 
        });
        if(iCounter == 0)
        {
            $('#<%=lblNoRecords.ClientID%>').css('display','');
        }
        e.preventDefault();
    })
})
</script>

该应用适用于图层,这适用于获取数据:

public List<Cajas> Cajas_Buscar_CajaXCont(string clienteCodigo, string cajaContenido)
{
    List<Cajas> ListCajasxContenido = new List<Cajas>();
    string storedProcedure = "CAJAS_BUSCAR_CAJAXCONT";

    using (DbConnection con = dpf.CreateConnection())
    {
        con.ConnectionString = conStr;
        using (DbCommand cmd = dpf.CreateCommand())
        {
            cmd.Connection = con;
            cmd.CommandText = storedProcedure;
            cmd.CommandType = CommandType.StoredProcedure;

            DbParameter param1 = cmd.CreateParameter();
            param1.DbType = DbType.String;
            param1.ParameterName = "CLIENTE_CODIGO";
            param1.Value = clienteCodigo;
            cmd.Parameters.Add(param1);

            DbParameter param2 = cmd.CreateParameter();
            param2.DbType = DbType.String;
            param2.ParameterName = "CONTENIDO_CAJA";
            param2.Value = cajaContenido;
            cmd.Parameters.Add(param2);

            con.Open();
            using (DbDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    try
                    {
                        //creamos un objeto de tipo Caja, y por c/ registro encontrado en la db, insertamos
                        Cajas cajaBus = new Cajas();

                        cajaBus.ClienteCodigo = dr["CLIENTE"].ToString();
                        cajaBus.CajaCodigo = dr["CODIGO CAJA"].ToString();
                        cajaBus.CajaNumero = Convert.ToInt16(dr["NUM"].ToString());
                        cajaBus.CajaContenido = dr["CONTENIDO"].ToString();


                        if (Convert.IsDBNull(dr["F.EMI"].ToString())) //para manejar los valores DBnull
                        {
                            cajaBus.FecEmisionDoc = Convert.ToDateTime("9999-01-01"); //de lo contrario c# no los podra interpretar
                        }
                        else
                        {
                            cajaBus.FecEmisionDoc = Convert.ToDateTime(dr["F.EMI"]);
                        }

                        ListCajasxContenido.Add(cajaBus);
                    }
                    catch (System.InvalidCastException)
                    { }
                    catch (System.FormatException)
                    { }
                }
            }


        }
    }
    return ListCajasxContenido;

}

我在演示文稿中调用了数据:

protected void sacbus_Consul_Cajas_x_Cont()
{
    gvwSacBus_Docs.DataSource = null;
    gvwSacBus_Docs.DataBind();
    gvwSacBus_Docs.DataSource = cajaBL.sacbus_Consul_Cajas_x_Cont(lbl_SacBus_CliCod.Text,
                                                        txt_SacBus_Criter.Text);
    gvwSacBus_Docs.DataBind();

    if (gvwSacBus_Docs.Rows.Count == 0)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "AlertScript", "alert('No Encontrado!');", true);
    }

}

这是用于过滤的文本框

<asp:TableRow
                                    ID="TableRow2"
                                    runat="server"
                                    BackColor="OrangeRed">
                                    <asp:TableCell>
                                        <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
                                    </asp:TableCell>
                                    <asp:TableCell>
                                        <asp:Button ID="Button1" runat="server" Text="Button"  />
                                    </asp:TableCell>
                                    <asp:TableCell>
                                        <asp:Label ID="lblNoRecords" Text="No records to display" runat="server" ForeColor="red"></asp:Label>
                                    </asp:TableCell>
                                </asp:TableRow>

这是我的网格(使用ItemTemplates)

<asp:GridView ID="gvwSacBus_Docs" runat="server" AutoGenerateColumns="false"
                                CssClass="mGrid" PagerStyle-CssClass="pgr" 
                                AlternatingRowStyle-CssClass="alt" Font-Size="Small"
                                AllowPaging="true" PageSize="100" OnPageIndexChanging="gvwCajNuev_PageIndexChanging" 
                                AllowSorting="true" OnSorting="gvwSacBus_Docs_Sorting">
                                <Columns>
                                    <asp:TemplateField>
                                        <HeaderTemplate>
                                            <asp:CheckBox ID = "chkAll_cajas" runat="server" AutoPostBack="true"  
                                                OnCheckedChanged="chkAll_cajas_CheckedChanged" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:CheckBox ID="chkCaja" runat="server" AutoPostBack="true"  />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <a href="#" onclick="window.open('Cajas_Historial.aspx?cliCod= <%#Eval("ClienteCodigo").ToString() 
                                                + "&cajCod=" + Eval("CajaCodigo").ToString()
                                                + "&cajNum=" + Eval("CajaNumero").ToString()
                                                %> ','PrintMe','height=500px,width=1300px,scrollbars=1');">Hist</a>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="CLIENTE">
                                        <ItemTemplate>
                                            <asp:Label ID = "lblSacBus_CliCod" runat="server" Text='<%# Eval("ClienteCodigo") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="COD.CAJA">
                                        <ItemTemplate>
                                            <asp:Label ID = "lblSacBus_CajCod" runat="server" Text='<%# Eval("CajaCodigo") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>

等 等等 等

正如您所看到的,我的问题是在filterig上使用te分页,我希望有人能帮助我。

最好的问候......

1 个答案:

答案 0 :(得分:0)

即使您的存储过程获取所有数据,服务器(ASP.Net)端的

GridView页也是如此。由于您通过在客户端隐藏记录进行过滤,因此它仅限于当前页面。我建议你过滤服务器端的数据(如果你可以在存储的proc / DB中过滤它,那么性能会更好),并将结果提供给GridView来正确过滤。