In this article I will explain with an example, how to use the PageIndexChanging event for implementing Paging in DetailsView control in ASP.Net.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The following HTML Markup consists of an ASP.Net DetailsView control with three BoundField columns.
AutoGenerateRows
This property is used to disable automatically display of records from database. Generally when BoundField or TemplateField columns are used, this property needs to be set to false as by default the value is true.
AllowPaging and OnPageIndexChanging event
In order to enable paging in the DetailsView control, the AllowPaging property needs to be set to true and also the OnPageIndexChanging event needs to be handled as we are populating the DetailsView from code behind.
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false" AllowPaging="true" OnPageIndexChanging = "OnPageIndexChanging">
<Fields>
<asp:BoundField DataField="CustomerId" HeaderText="Customer Id" HeaderStyle-CssClass="header" />
<asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-CssClass="header" />
<asp:BoundField DataField="Country" HeaderText="Country" HeaderStyle-CssClass="header" />
</Fields>
</asp:DetailsView>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Populating DetailsView from Code Behind without using SqlDataSource
Inside the Page Load event of the page, the DetailsView is populated with records from the Customers table.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindDetailsView();
}
}
private void BindDetailsView()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
DetailsView1.DataSource = dt;
DetailsView1.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindDetailsView()
End If
End Sub
Private Sub BindDetailsView()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT CustomerId, Name, Country FROM Customers")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
DetailsView1.DataSource = dt
DetailsView1.DataBind()
End Using
End Using
End Using
End Using
End Sub
Implementing Paging in DetailsView
The following event handler is executed when a page is changed inside the DetailsView.
The value of the PageIndex of the Page which was clicked is present inside the NewPageIndex property of the DetailsViewPageEventArgs object and it is set to the PageIndex property of the DetailsView and the DetailsView is again populated by calling the BindDetailsView function.
C#
protected void OnPageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
this.BindDetailsView();
}
VB.Net
Protected Sub OnPageIndexChanging(sender As Object, e As DetailsViewPageEventArgs)
DetailsView1.PageIndex = e.NewPageIndex
Me.BindDetailsView()
End Sub
Screenshot
Demo
Downloads