NeedDataSource event handler.
Other people have run into this issue:
http://www.telerik.com/community/forums/aspnet-ajax/grid/page-count-inaccurate-after-new-search.aspx#0
http://www.telerik.com/community/forums/aspnet-ajax/grid/rebind-not-updating-pager.aspx
However, the suggestion of setting EnableLinqExpressions to false did not work for me. Does anyone have any other suggestions to try? I'm running on .NET 3.5 and using RadControls for ASP.NET AJAX Q3 2009 NET3.5.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SearchPersonControl.ascx.cs" Inherits="SearchPersonControl" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<%@ Register assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" namespace="System.Web.UI.WebControls" tagprefix="asp" %>
<telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
<asp:Table ID="tblMainLayout" runat="server">
<asp:TableRow>
<asp:TableCell>
<asp:Label ID="lblPerson" runat="server" Text="Persoon:"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<telerik:RadTextBox ID="txtPersonSearch" Runat="server" Width="200px"></telerik:RadTextBox>
</asp:TableCell>
<asp:TableCell>
<asp:Button ID="btnSearch" runat="server" onclick="btnSearch_Click" Text="Search" />
</asp:TableCell>
<asp:TableCell>
<asp:Button ID="btnAdvancedSearch" runat="server" onclick="btnAdvancedSearch_Click" Text="Show Advanced Search" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="tblrAdvancedSearch1" runat="server" Visible="false">
<asp:TableCell>
<asp:Label ID="lblID" runat="server" Text="ID nummer: "></asp:Label>
</asp:TableCell>
<asp:TableCell>
<telerik:RadTextBox ID="txtID" Runat="server"></telerik:RadTextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="tblrAdvancedSearch2" runat="server" Visible="false">
<asp:TableCell>
<asp:Label ID="lblName" runat="server" Text="Naam:"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<telerik:RadTextBox ID="txtName" Runat="server" Width="200px"></telerik:RadTextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell ColumnSpan="4">
<telerik:RadGrid ID="grdPersons" runat="server" AllowPaging="True"
DataSourceID="HECINA_DEV_M_SearchPersonEntities" GridLines="None"
AllowSorting="True" AllowFilteringByColumn = "true" OnNeedDataSource="grdPersons_NeedDataSource"
EnableLinqExpressions="false">
<MasterTableView AutoGenerateColumns="False"
DataSourceID="HECINA_DEV_M_SearchPersonEntities">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<SortExpressions>
<telerik:GridSortExpression FieldName="Persons_FullName" SortOrder="Ascending" />
</SortExpressions>
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="Persons_FullName" DefaultInsertValue=""
HeaderText="Naam" ReadOnly="True" SortExpression="Persons_FullName"
UniqueName="Persons_FullName"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Persons_BirthDate"
DataType="System.DateTime" DefaultInsertValue="" HeaderText="Geboortedatum"
ReadOnly="True" SortExpression="Persons_BirthDate"
UniqueName="Persons_BirthDate"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Persons_IdentificationNumber"
DefaultInsertValue="" HeaderText="Identificatienummer" ReadOnly="True"
SortExpression="Persons_IdentificationNumber"
UniqueName="Persons_IdentificationNumber"></telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:EntityDataSource ID="HECINA_DEV_M_SearchPersonEntities" runat="server"
ConnectionString="name=HECINA_DEV_M_SearchPersonEntities"
DefaultContainerName="HECINA_DEV_M_SearchPersonEntities"
EntitySetName="Persons" EntityTypeFilter="Persons"
Select="it.[Persons_BirthDate], it.[Persons_FullName], it.[Persons_IdentificationNumber]">
</asp:EntityDataSource>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CX.Hecina.DataAccessLayer;
public partial class SearchPersonControl : System.Web.UI.UserControl
{
HECINA_DEV_M_SearchPersonEntities searchPersonEntities;
protected void Page_Load(object sender, EventArgs e)
{
this.searchPersonEntities = new HECINA_DEV_M_SearchPersonEntities();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
//Only one of these can be set. Always clear both to ensure the data grid is current.
this.grdPersons.DataSourceID = null;
this.grdPersons.DataSource = null;
this.grdPersons.CurrentPageIndex = 0;
this.grdPersons.Rebind();
}
protected void grdPersons_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
//Always default to the full data set. This is useful if no search terms were specified.
IQueryable<Persons> qry = this.searchPersonEntities.Persons.OrderBy(p => p.Persons_FullName);
if (this.txtName.Text.Length != 0)
{
qry = this.searchPersonEntities.Persons.Where(p => p.Persons_FullName.ToUpper().Contains(this.txtName.Text.ToUpper()));
}
if (this.txtID.Text.Length != 0)
{
qry = this.searchPersonEntities.Persons.Where(p => p.Persons_IdentificationNumber.ToUpper().Contains(this.txtID.Text.ToUpper()));
}
//Google-like search that matches on multiple fields simultaneously.
if (this.txtPersonSearch.Text.Length != 0)
{
string searchText = this.txtPersonSearch.Text.ToUpper();
qry = this.searchPersonEntities.Persons.Where(p => p.Persons_FullName.ToUpper().Contains(searchText) ||
//TODO: Figure this out, at runtime an exception is thrown that LINQ does not
//recognize ToShortDateString().
//p.Persons_BirthDate.ToShortDateString().Contains(searchText) ||
p.Persons_IdentificationNumber.ToUpper().Contains(searchText));
}
//TODO: figure out how to update record and page count.
this.grdPersons.DataSource = qry;
}
protected void btnAdvancedSearch_Click(object sender, EventArgs e)
{
//Simply invert the visibility property value of the advanced search fields.
this.tblrAdvancedSearch1.Visible ^= true;
this.tblrAdvancedSearch2.Visible ^= true;
if (this.tblrAdvancedSearch1.Visible == true)
{
this.btnAdvancedSearch.Text = "Hide Advanced Search";
}
else
{
this.btnAdvancedSearch.Text = "Show Advanced Search";
}
}
}
The only thing I could think I'm doing differently is that I'm using the grid inside a user control.