Asp.Net Custom Paging in Datalist Control
Asp.Net Custom Paging in Datalist Control
Step 1- Datalist Control and Two div as given below<div class="PagingClass" id="Upperpaging" runat="server" align="right">
<table width="300px">
<tr>
<td>
<asp:LinkButton ID="lbtnFirst2" BackColor="#333333" SkinID="PageLnk" Text="First"
runat="server" OnClick="lbtnFirst2_Click"></asp:LinkButton>
</td>
<td>
<asp:ImageButton ID="lbtnPrivo2" ImageUrl="~/Images/Prious.png" runat="server" OnClick="lbtnPrivo2_Click">
</asp:ImageButton>
</td>
<td>
<asp:Label ID="lblcurrentpage2" runat="server"></asp:Label>
</td>
<td>
<asp:ImageButton ID="lbtnNext2" ImageUrl="~/Images/Next.png" runat="server" OnClick="lbtnNext2_Click">
</asp:ImageButton>
</td>
<td>
<asp:LinkButton ID="lbtnLast2" BackColor="#333333" Text="Last" SkinID="PageLnk" runat="server"
OnClick="lbtnLast2_Click"></asp:LinkButton>
</td>
</tr>
</table>
</div>
<asp:DataList ID="dlUnanswered" runat="server" Width="100%" DataKeyNames="AskedBy">
<ItemTemplate>
<table width="100%">
<tr>
<td>
<a id="lnkQuestion" href='<%# String.Format("~/CommentReply.aspx?QuestionId={0}", Eval("QuestionId")) %>'
runat="server" class="question">
<%# Eval("Questions") %></a>
</td>
</tr>
<tr>
<table width="100%">
<tr>
<td align="left" style="padding-right: 100px;" width="600px">
<asp:Label ID="lblAskedby" runat="server" Font-Size="Small" ForeColor="gray" Text="Asked by:"></asp:Label>
<a id="lnkAskedBy" href='<%# String.Format("~/ViewUserProfile.aspx?ID={0}", Eval("AskedBy")) %>'
runat="server" class="userlink">
<%# Eval("Name")%></a>
</td>
</tr>
</table>
</tr>
</table>
<hr class="line" style="margin-top: 1px" />
</ItemTemplate>
</asp:DataList>
<div class="PagingClass" id="Topppaging" runat="server" align="right">
<table width="300px">
<tr>
<td>
<asp:LinkButton ID="lbtnFirst" BackColor="#333333" Text="First" SkinID="PageLnk"
runat="server" OnClick="lbtnFirst_Click"></asp:LinkButton>
</td>
<td>
<asp:ImageButton ID="lbtnPrivo" ImageUrl="~/Images/Prious.png" runat="server" OnClick="lbtnPrivo_Click">
</asp:ImageButton>
</td>
<td>
<asp:Label ID="lblcurrentpage" runat="server"></asp:Label>
</td>
<td>
<asp:ImageButton ID="lbtnNext" ImageUrl="~/Images/Next.png" runat="server" OnClick="lbtnNext_Click">
</asp:ImageButton>
</td>
<td>
<asp:LinkButton ID="lbtnLast" BackColor="#333333" SkinID="PageLnk" Text="Last" runat="server"
OnClick="lbtnLast_Click"></asp:LinkButton>
</td>
</tr>
</table>
</div>
Step 2 . demo.aspx.cs
public int CurrentPage
{
get
{
object obj = this.ViewState["_CurrentPage"];
if (obj == null)
{
return 0;
}
else
{
return (int)obj;
}
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}
protected void lbtnFirst_Click(object sender, EventArgs e)
{
CurrentPage = 0;
BindUnAnsweredQuestion();
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
BindUnAnsweredQuestion();
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
}
protected void lbtnPrivo_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
BindUnAnsweredQuestion();
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
}
protected void lbtnLast_Click(object sender, EventArgs e)
{
CurrentPage = BindUnAnsweredQuestion() - 1;
BindUnAnsweredQuestion();
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
}
protected void lbtnNext2_Click(object sender, ImageClickEventArgs e)
{
CurrentPage += 1;
BindUnAnsweredQuestion();
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
}
protected void lbtnPrivo2_Click(object sender, ImageClickEventArgs e)
{
CurrentPage -= 1;
BindUnAnsweredQuestion();
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
}
protected void lbtnLast2_Click(object sender, EventArgs e)
{
CurrentPage = BindUnAnsweredQuestion() - 1;
BindUnAnsweredQuestion();
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
}
protected void lbtnFirst2_Click(object sender, EventArgs e)
{
CurrentPage = 0;
BindUnAnsweredQuestion();
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
}
step 3- Method show be int return type
public int BindUnAnsweredQuestion()
{
int count = 0;
try
{
var query = (from obj in dc.SP_GET_UNANSWERED_QUESTION(500)
select new
{
obj.QuestionId,
Questions = obj.DETAIL,
Name = obj.FIRSTNAME,
obj.AskedBy,
}).ToList();
if (query.Count > 0)
{
int iPageSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PageSize"]);
PagedDataSource objpds = new PagedDataSource();
objpds.DataSource = query;
objpds.AllowPaging = true;
objpds.PageSize = iPageSize;
objpds.CurrentPageIndex = CurrentPage;
count = objpds.PageCount;
lblcurrentpage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + objpds.PageCount.ToString();
lblcurrentpage2.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + objpds.PageCount.ToString();
ViewState["totalpages"] = objpds.PageCount;
lbtnFirst.Enabled = !objpds.IsFirstPage;
lbtnFirst2.Enabled = !objpds.IsFirstPage;
lbtnPrivo.Enabled = !objpds.IsFirstPage;
lbtnPrivo2.Enabled = !objpds.IsFirstPage;
lbtnNext.Enabled = !objpds.IsLastPage;
lbtnNext2.Enabled = !objpds.IsLastPage;
lbtnLast.Enabled = !objpds.IsLastPage;
lbtnLast2.Enabled = !objpds.IsLastPage;
lbtnFirst.Enabled = !objpds.IsFirstPage;
lbtnFirst2.Enabled = !objpds.IsFirstPage;
dlUnanswered.DataSource = objpds;
dlUnanswered.DataBind();
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}