根据输入到TextBox中的值从gridview搜索记录

时间:2015-08-24 12:09:21

标签: javascript asp.net linq gridview pagination

我正在尝试使用分页从gridview中搜索记录,与输入到文本框中的值匹配,并且它的工作非常好。问题是它只显示第一页的记录,但不会搜索下一页的记录。

aspx代码:

<asp:GridView ID="GrdFutureApt" AutoGenerateColumns="false" runat="server" CssClass="table table-responsive table-condensed table-bordered table-striped" AllowPaging="true" 
         CellPadding="4" ForeColor="#333333" GridLines="None" PageSize="5" OnPageIndexChanging="GrdFutureApt_PageIndexChanging">

         <Columns>
              <asp:TemplateField>
                    <ItemTemplate>
                        <%#Container.DataItemIndex+1 %>
                    </ItemTemplate>
                </asp:TemplateField>
             <asp:BoundField DataField="PatientName" HeaderText="Patient Name" Visible="false"/>
             <asp:BoundField DataField="DoctorName" HeaderText="Doctor Name"/>
             <asp:BoundField DataField="AppointmentDate" HeaderText="Appointment Date" DataFormatString="{0:dd/MM/yyyy}"/>

             <asp:TemplateField HeaderText = "Appointment Time" SortExpression="Time">
                    <ItemTemplate>
                    <asp:Label runat="server" ID="lblAppointmentTime" Text='<%# DisplayAs12HourTime(Eval("AppointmentTime")) %>'></asp:Label>

                  </ItemTemplate> 
               </asp:TemplateField>
             <asp:BoundField DataField="AppoinmentStatus" HeaderText="Status"/>
         </Columns>
         <PagerSettings  Mode="NextPrevious" PreviousPageText="Previous" NextPageText="&nbsp; Next"  Position="Bottom" />
         <PagerStyle BackColor="#889FA2" HorizontalAlign="Left"  ForeColor="White" Font-Bold="true" />
     </asp:GridView>

我使用了以下脚本来执行此功能。

<script type="text/javascript">
    $(document).ready(function () {
        $('#<%=txtSearchBox.ClientID %>').keyup(function (e) {
            SearchGridData();

        });
    });
    function SearchGridData() {
        var counter = 0;
        //Get the search text
        var searchText = $('#<%=txtSearchBox.ClientID %>').val().toLowerCase();
        //Hide No record found message
        $('#<%=lblMsgFail.ClientID %>').hide();
        //Hode all the rows of gridview
        $('#<%=GrdAppointments.ClientID %> tr:has(td)').hide();
        if (searchText.length > 0) {
            //Iterate all the td of all rows
            $('#<%=GrdAppointments.ClientID %> tr:has(td)').children().each(function () {
                var cellTextValue = $(this).text().toLowerCase();
                //Check that text is matches or not
                if (cellTextValue.indexOf(searchText) >= 0) {
                    $(this).parent().show();
                    counter++;
                }
            });
            if (counter == 0) {
                //Show No record found message
                $('#<%=lblErrorMsg.ClientID %>').show();
            }
        }
        else {
            //Show All the rows of gridview
            $('#<%=lblErrorMsg.ClientID %>').hide();
            $('#<%=GrdAppointments.ClientID %> tr:has(td)').show();

        }
    }
</script>

并使用以下javascript

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>

填充gridview的数据来自以下函数:

public void LoadGrid()
{
    S011AppointmentBOL objBol = new S011AppointmentBOL();
    //DataTable ptApt = new DataTable();
    dtAppointment = S011AppointmentBLL.GetAll();

    int patID = dtPatient.AsEnumerable().Where(x => x.Field<string>("RegistrationNo") == RegNo).Select(x => x.Field<int>("PatientID")).FirstOrDefault();
    dtAppointment.Columns.Add("PatientName", typeof(string));
    dtAppointment.Columns.Add("DoctorName", typeof(string));
    for (int i = 0; i < dtAppointment.Rows.Count; i++)
    {
        string pFname = dtPatient.AsEnumerable()
                        .Where(x => x.Field<int>("PatientID") == Convert.ToInt32(dtAppointment.Rows[i]["PatientID"]))
                        .Select(x => x.Field<string>("FirstName")).FirstOrDefault();
        string pLname = dtPatient.AsEnumerable()
                        .Where(x => x.Field<int>("PatientID") == Convert.ToInt32(dtAppointment.Rows[i]["PatientID"]))
                        .Select(x => x.Field<string>("LastName")).FirstOrDefault();
        string dFname = dtDoctor.AsEnumerable()
                        .Where(x => x.Field<int>("DoctorID") == Convert.ToInt32(dtAppointment.Rows[i]["DoctorID"]))
                        .Select(x => x.Field<string>("FirstName")).FirstOrDefault();
        string dLname = dtDoctor.AsEnumerable()
                         .Where(x => x.Field<int>("DoctorID") == Convert.ToInt32(dtAppointment.Rows[i]["DoctorID"]))
                         .Select(x => x.Field<string>("LastName")).FirstOrDefault();
        dtAppointment.Rows[i]["PatientName"] = pFname + " " + pLname;
        dtAppointment.Rows[i]["DoctorName"] = dFname + " " + dLname;
    }
    DataTable boundTable = new DataTable();
    var query = dtAppointment.AsEnumerable().Where(x => x.Field<int>("PatientID") == patID).Select(x => x).OrderByDescending(x => x.Field<DateTime>("AppointmentDate"));
    var t = query.Any();
    if (t)
    {
        boundTable = query.CopyToDataTable<DataRow>();
    }

    GrdAppointments.DataSource = boundTable;
    GrdAppointments.DataBind();
}

并且在每个keyup上我都不想查询数据库,这就是为什么我使用Datatable填充gridview的原因  感谢任何帮助..谢谢

1 个答案:

答案 0 :(得分:0)

您已经为Gridview启用了分页,因此它只会绑定客户端所选页面的记录。因此,您必须通过调用webmethord来搜索可以绑定搜索结果的整个记录​​来禁用分页或重写jquery搜索功能。