Search engines will automatically reach the target page
and will index those pages in this case.
Moving forward, in this article we will
provide custom paging for GridView control that are search engine friendly.
Additionally, we will also provide jump to page feature using dropdownlist
control for the GridView control.
Note
We will use the same example which
binds Employee information in GridView control.
Search Engine Friendly Pager
We will use an ASP.Net panel control to
render the pager hyperlinks.
Steps
1. Open Visual Studio > Click File > New > Website > Select ASP.Net Website.
3. Drag a GridView control and panel control into our ASPX
page.
Refer my previous article, Custom
Paging in GridView Using LINQ to create LINQ to SQL classes. We will create
a data access class that has 2 methods, GetEmployeeCount() and BindEmployees().
The first method will fetch the count of employees available at that moment and
the former will fetch the records that belong to a particular page.
public class EmployeeDAO
{
public EmployeeDAO()
{
//
// TODO: Add constructor
logic here
//
}
public IQueryable BindEmployees(int
startRowIndex, int maximumRows)
{
EmployeeInfoDataContext dbEmp =
new EmployeeInfoDataContext();
var query = from emp in
dbEmp.Employees
join dept in
dbEmp.Departments
on emp.DeptID
equals dept.DeptID
select new
{
EmpID =
emp.EmpID,
EmpName =
emp.EmpName,
Age =
emp.Age,
Address =
emp.Address,
DeptName =
dept.DepartmentName
};
return
query.Skip(startRowIndex).Take(maximumRows);
}
public int
GetEmployeeCount()
{
EmployeeInfoDataContext dbEmp =
new EmployeeInfoDataContext();
return (from emp in
dbEmp.Employees
select emp).Count();
}
}
In the above code, the LINQ query that
fetches the employee record uses join operator to get the department name from
the Department table(Refer BindEmployees() method). We use Skip() and Take()
operator to fetch the records that belongs to the current page.
Next, we will use the above methods to
build the pager links with hyperlinks and bind the current page.
The below code does that,
public partial class GVPagingUsingLINQ :
System.Web.UI.Page
{
int PageSize = 2;
protected void Page_Load(object
sender, EventArgs e)
{
EmployeeDAO daoEmp = new
EmployeeDAO();
int TotalRows =
daoEmp.GetEmployeeCount();
int CurrentPage;
bool res =
int.TryParse(Request.QueryString["Page"], out CurrentPage);
if (!res)
{
CurrentPage = 1;
}
int PageCount = TotalRows /
PageSize;
if (PageCount * PageSize !=
TotalRows)
{
PageCount = PageCount +
1;
}
HyperLink hplPage =
null;
Literal ltSpace = null;
for (int i = 1; i <= PageCount;
i++)
{
hplPage = new
HyperLink();
ltSpace = new
Literal();
ltSpace.Text =
" ";
hplPage.Text =
i.ToString();
//make the current page non
clickable
if (CurrentPage !=
i)
{
hplPage.NavigateUrl =
Request.CurrentExecutionFilePath + "?Page=" + i.ToString();
}
plPager.Controls.Add(hplPage);
plPager.Controls.Add(ltSpace);
}
int StartIndex = (CurrentPage - 1)
* PageSize;
gvEmployee.DataSource =
daoEmp.BindEmployees(StartIndex, PageSize);
gvEmployee.DataBind();
}
|