1- I'll Show You how to make a repeater that allow paging.
2- I'll Make a web user control and anyone Can use it in his project.
Steps:
1- Create A web user control using C# and rename it to RepeaterPager.
2- add four Buttons and one label:
Control Name Text
Button btnNext >
Button btnPrevious <
Button btnLastRecord >>
Button btnFirstRecord <<
3- Create property for Object Data Source:
public ObjectDataSource Ods { get; set; }
4- Create Property for Repeater Control Or DataList As you like:
public Repeater Rep { get; set; }
5- Create Property Get Current Page:
public int CurrentPage
{
get
{
object obj = this.ViewState["_CurrentPage"];
if (obj == null)
{
return 0;
}
else
{
return (int)obj;
}
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}
6- Bind Data in Repeater Control and Return number of Pages:
public int ItemsGet()
{
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = Ods.Select();
int x = objPds.Count;
objPds.AllowPaging = true;
objPds.PageSize = 1;
objPds.CurrentPageIndex = CurrentPage;
if (objPds.Count > 0)
{
btnPrevious.Visible = true;
btnNext.Visible = true;
btnLastRecord.Visible = true;
btnFirstRecord.Visible = true;
lblCurrentPage.Visible = true;
lblCurrentPage.Text = "Page: " + Convert.ToString(CurrentPage + 1) + " of " + Convert.ToString(objPds.PageCount);
}
else
{
btnPrevious.Visible = false;
btnNext.Visible = false;
btnLastRecord.Visible = false;
btnFirstRecord.Visible = false;
lblCurrentPage.Visible = false;
}
btnPrevious.Enabled = !objPds.IsFirstPage;
btnNext.Enabled = !objPds.IsLastPage;
btnLastRecord.Enabled = !objPds.IsLastPage;
btnFirstRecord.Enabled = !objPds.IsFirstPage;
Rep.DataSource = objPds;
Rep.DataBind();
return x;
}
7- Buttons Controls Code:
protected void btnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
ItemsGet();
}
protected void btnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
ItemsGet();
}
protected void btnLastRecord_Click(object sender, EventArgs e)
{
CurrentPage = ItemsGet() -1;
ItemsGet();
}
protected void btnFirstRecord_Click(object sender, EventArgs e)
{
CurrentPage = 0;
ItemsGet();
}
8- in (Web User Control) Load Event call GetItems Property:
protected void Page_Load(object sender, EventArgs e)
{
ItemsGet();
}
How To Use this Control:
1- Create ASP.Net Page
2- Add Repeater1 and ObjectDataSource1 to your page
3- in Page_load Event Put the following code:
protected void Page_Load(object sender, EventArgs e)
{
RepeaterPager.Ods= Repeater1;
RepeaterPager.Rep= ObjectDataSource1;
}
4- Build and run :)
No comments:
Post a Comment