How to paginate records in DetailsView?

SheoNarayan
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 3616 red flag
Rating: 5 out of 5  
 1 vote(s)

To show the complete details about the record with pagination so that user would be able to navigate between the records, we can DetailsView. In this article we are going to learn how to use DetailsView control.

DetailsView

DetailsView control is another data-bound control that helps us to display a single record at a time. It also gives us flexibility to manipulate, paginate the records.

Get hundreds of ASP.NET Tips and Tricks and ASP.NET Online training here.

In the previous article, we learnt about How to show a record using DetailsView in ASP.NET? In this article,we shall learn how to paginate records into DetailsView?

ASPX PAGE

 

<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"CellPadding="4" OnPageIndexChanging="ChangeRecord" ForeColor="#333333"GridLines="None" >

<AlternatingRowStyle BackColor="White" />

<CommandRowStyle BackColor="#FFFFC0" Font-Bold="True" />

<FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />

<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />

<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />

</asp:DetailsView>

In order to paginate the DetailsView, we need to set AllowPaging property to true and handle the
OnPageIndexChanging event. Rest all styles related children elements of DetailsView helps to decorate the DetailsView control.

CODE BEHIND

string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;


protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

PopulateData();

}

}

 

private void PopulateData()

{

DataTable table = new DataTable();

// get the connection

using (SqlConnection conn = new SqlConnection(_connStr))

{

// write the sql statement to execute

string sql = "SELECT AutoId, FirstName, LastName, Age, Active FROM PersonalDetail ORDER By AutoId";

// instantiate the command object to fire

using (SqlCommand cmd = new SqlCommand(sql, conn))

{

// get the adapter object and attach the command object to it

using (SqlDataAdapter ad = new SqlDataAdapter(cmd))

{

// fire Fill method to fetch the data and fill into DataTable

ad.Fill(table);

}

}

}

// specify the data source for the DetailsView

DetailsView1.DataSource = table;

// bind the data now

DetailsView1.DataBind();

}


/// <summary>

/// Changes the record.

/// </summary>

/// <param name="sender">The sender.</param>

/// <param name="e">The <see cref="System.Web.UI.WebControls.DetailsViewPageEventArgs"/> instance containing the event data.</param>

 

protected void ChangeRecord(object sender, DetailsViewPageEventArgs e)

{

DetailsView1.PageIndex = e.NewPageIndex;

this.PopulateData();

}

In the above code snippet, we have attached ChangeRecord method with OnPageIndexChanging event of the DetailsView in which we have set the new page index to the Page index property of the DetailsView and re-populated the data.

OUTPUT

Note:

Other data manipulations operations can be performed in the similar way as we do in the GridView and other Data controls.

Hope this article was useful. Thanks for reading, hope you liked it.

Keep reading my forth coming articles. To read my series of articles on ASP.NET,click here.

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Posted by: Parcha on: 11/8/2011 | Points: 25
Hi Sheo
Your articles are awesome .
i never missed a single article of you i learned a lot from your articles
Thanks
Karthik

Login to post response

Comment using Facebook(Author doesn't get notification)