Pagination in DataList

Johnbhatt
Posted by Johnbhatt under ASP.NET category on | Points: 40 | Views : 1545
ASPX Code:
<div>
<asp:DataList ID="DataList1" runat="server" Height="194px" Width="174px"
oneditcommand="DataList1_EditCommand"
oncancelcommand="DataList1_CancelCommand"
onupdatecommand="DataList1_UpdateCommand" DataKeyField="eno">
<HeaderTemplate>
<table width="450" border="2">
<tr>
<th>Employee Name </th>
<th>Designation </th>
<th>Salary </th>
<th>
Edit
</th>
</tr>
</HeaderTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="txtEname" Text='<%#Eval("ename")%>' runat="server" />
</td>
<td>
<asp:TextBox ID="txtJOb" Text='<%#Eval("job")%>' runat="server" />
</td>
<td>
<asp:TextBox ID="txtSal" Text='<%#Eval("sal")%>' runat="server" />
</td>
<td>
<asp:LinkButton ID="lnk2" runat="server" Text="Update" CommandName ="Update" />
</td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Cancel"
CommandName ="Cancel" />
</td>
</tr>
</EditItemTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("ename")%></td>
<td><%#Eval("job")%></td>
<td><%#Eval("sal")%></td>
<td>
<asp:LinkButton ID="lnk1" CommandName="Edit" Text="Edit !" runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
<br />
</div>
<table class="style1">
<tr>
<td class="style2">
<asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">Next</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton3" runat="server" onclick="LinkButton3_Click">Previous</asp:LinkButton>
</td>
</tr>
</table>

ASPX.CS Code.

    protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//ViewState["pg"] = 0;
CurrentPageIndex = 0;
showData();
}
}
void showData()
{
PagedDataSource pgd = new PagedDataSource();
SqlDataAdapter da = new SqlDataAdapter("select * from emp", "Data Source=DOTNET-PC\\SQLEXPRESS;Initial Catalog=batch10;Integrated Security=True");
DataSet ds = new DataSet();
da.Fill(ds);
pgd.DataSource = ds.Tables[0].DefaultView;
pgd.CurrentPageIndex = CurrentPageIndex ;
pgd.AllowPaging = true;
pgd.PageSize = 5;
LinkButton2.Enabled = !(pgd.IsLastPage);
LinkButton3.Enabled = !(pgd.IsFirstPage);
DataList1.DataSource = pgd;
DataList1.DataBind();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
showData();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
showData();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{

}
public int CurrentPageIndex
{
get
{
if (ViewState["pg"] == null)
return 0;
else
return Convert.ToInt16(ViewState["pg"]);
}
set
{
ViewState["pg"] = value;
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
CurrentPageIndex++;
showData();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
CurrentPageIndex--;
showData();
}

Comments or Responses

Login to post response