how to do paging inside the datalist control
i need to do paging for a datalist control. i w开发者_Python百科ill be displaying (6 columns* 5 rows). so once the record is exceed it should go to next page can any one give the code how tio implement this one. as we dnt hvae a built in paging for datalist control thank you
Check out the RepeatColumns property of the DataList control, paging can be done in classical way. :
DataList1.RepeatColumns = 6;
DataList1.RepeatDirection = RepeatDirection.Horizontal;
For Datalist Paging use PagedDataSource, PagedDataSource is used to provide a paging facility to data-bound controls such as DataList, Repeater, DataGrid, GridView, DetailsView and FormView that allow it to perform paging. For Example Create SQL Table
CREATE TABLE [dbo].[tbl_category](
[catid] [int] IDENTITY(1,1) NOT NULL,
[categoryname] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_tbl_category] PRIMARY KEY CLUSTERED
(
[catid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Default.aspx Page contain
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("categoryname") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
<asp:label id="lblCurrentPage" runat="server"></asp:label>
<asp:LinkButton ID="lbtnPrev"
Runat="server" onclick="lbtnPrev_Click">Prev</asp:LinkButton>
<asp:LinkButton ID="lbtnNext"
Runat="server" onclick="lbtnNext_Click">Next</asp:LinkButton>
Next Code Behind
protected void Page_Load(object sender, EventArgs e)
{
ItemsGet();
}
private void ItemsGet()
{
string qry = "select catid, categoryname from tbl_category";
SqlConnection con = new SqlConnection(a.dbConnection());
SqlDataAdapter da = new SqlDataAdapter(qry, con);
DataTable dt = new DataTable();
da.Fill(dt);
PagedDataSource pgsrc = new PagedDataSource();
pgsrc.DataSource = dt.DefaultView;
pgsrc.AllowPaging = true;
pgsrc.PageSize = 3;
pgsrc.CurrentPageIndex = CurrentPage;
DataList1.DataSource = pgsrc;
DataList1.DataBind();
lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + pgsrc.PageCount.ToString();
}
public int CurrentPage
{
get
{
// look for current page in ViewState
object o = this.ViewState["_CurrentPage"];
if (o == null)
return 0; // default page index of 0
else
return (int)o;
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}
protected void lbtnPrev_Click(object sender, EventArgs e)
{
// Set viewstate variable to the previous page
CurrentPage -= 1;
// Reload control
ItemsGet();
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
// Set viewstate variable to the next page
CurrentPage += 1;
// Reload control
ItemsGet();
}
精彩评论