Asp.net Data Pager control



DataPager is a new added control in ASP.NET 3.5 that provides paging functionality for data-bound control that implements IPJBlittem Container interface, such as System.Web.UI.WebControls.ListView Control

Generally, we need to enable ViewState to list viewview control for the list view, although Quatre Stringfield is a feature in the DataPager control, which specifies the name of the query string that we want to endorse ListView Want to use. Say your penzem is DataPager.aspx and we have written DataPager control for our ListView like this

<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="5"
QueryStringField="pageid">
<Fields>
<asp:NumericPagerField ButtonCount="5" />
</Fields>
</asp:DataPager>

Image result for Asp.net Data Pager control

Now lets see the complete code to paginate. Complete code is available for download from the top-right of the page.
DataPager.aspx page
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="PlaceHolder1" DataKeyNames="AutoId"
OnPagePropertiesChanging="PagePropertiesChanging">

<LayoutTemplate>


<table width="100%" cellpadding="4" cellspacing="0">

<tr class="header">
<th style="width: 30%;">
Name
</th>
<th style="width: 50%;">
Address
</th>
<th style="width: 20;">
Phone
</th>
</tr>

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

</table>

</LayoutTemplate>

<ItemTemplate>


<tr class="item">

<td>

<%# Eval("Name") %>
</td>

<td>

<%# Eval("Address") %>
</td>

<td>

<%# Eval("Phone") %>
</td>

</tr>

</ItemTemplate>

<AlternatingItemTemplate>


<tr>

<td>

<%# Eval("Name") %>
</td>

<td>

<%# Eval("Address") %>
</td>

<td>

<%# Eval("Phone") %>
</td>

</tr>

</AlternatingItemTemplate>

<EmptyDataTemplate>


<hr />

No Records Found.<hr />

<asp:LinkButton ID="lnkInsert" runat="server" Text="Insert Records" CommandName="Insert" />

</EmptyDataTemplate>
</asp:ListView>
<div style="text-align: center; font-weight: bold;">
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="5"
QueryStringField="pageid">
<Fields>
<asp:NumericPagerField ButtonCount="5" />
</Fields>
</asp:DataPager>
</div>
Things to notice in the above code snippet is the OnPagePropertiesChanging="PagePropertiesChanging" attribute of ListView andQueryStringField="pageid" attribute of DataPager control. Rest of the code is notihing much special. When page number link will be clicked PagePropertiesChanging method will fire.

DataPager.aspx.cs
string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindPersonDetails("Name ASC");
}
}
/// <summary>
/// Fires when page links are clicked in the Page control
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
// Rebind the data

BindPersonDetails("Name ASC");
}
/// <summary>
/// Bind Person Details data
/// </summary>
private void BindPersonDetails(string sortExpression)
{
sortExpression = sortExpression.Replace("Ascending", "ASC");
using (SqlConnection conn = new SqlConnection(_connStr))
{
conn.Open();
using (SqlDataAdapter dAd = new SqlDataAdapter("select * from Details order by Name", conn))
{
DataTable dTable = new DataTable();
dAd.Fill(dTable);
// Sort now

dTable.DefaultView.Sort = sortExpression;

// Bind data now

ListView1.DataSource = dTable;
ListView1.DataBind();
}
conn.Close();
}
}
Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment