<div style="margin:auto">
current page: <asp:Label ID="lbCurrent" runat="server"></asp:Label>
<br />
Total pages: <asp:Label ID="lbTotal" runat="server"></asp:Label>
<asp:DataList ID="dlProduct" runat="server" OnItemCommand="dlProduct_ItemCommand">
<ItemTemplate>
<%#Eval("Pname") %>
<br />
<%#Eval("Pcolor") %>
<br />
<%#Eval("Pnewprice") %>
<br />
<%#Eval("Pcontent") %>
<br />
<asp:Button ID="btnDelectProduct" runat="server" Text="Delete"
CommandName="delete" CommandArgument='<%#Eval("Pid") %>' />
<br />
<br />
</ItemTemplate>
<SelectedItemTemplate>
<%#Eval("Pname") %>
<br />
<%#Eval("Pcolor") %>
<br />
<%#Eval("Pnewprice") %>
<br />
<%#Eval("Pcontent") %>
<br />
<asp:Button ID="btnDelectProduct" runat="server" Text="Delete"
CommandName="delete" CommandArgument='<%#Eval("Pid") %>' />
<br />
<img alt="." height="100" src="Image/M.jpg"/>
</SelectedItemTemplate>
</asp:DataList>
<br />
<asp:LinkButton ID="lbPrevious" runat="server" OnCommand="IndexChange" CommandArgument="previous"><</asp:LinkButton>
<asp:LinkButton ID="lbNext" runat="server" OnCommand="IndexChange" CommandArgument="next">></asp:LinkButton>
</div>
public partial class dlPage : System.Web.UI.Page
{
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["pageindex"] = "0";
BindData();
}
}
protected void PrepareData()
{
SqlConnection conn = ProjectDal.GetConnection();
conn.Open();
string sql = "select * from Tb_product";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
conn.Close();
}
protected void BindData()
{
PrepareData();
DataTable dt = ds.Tables[0];
if (dt != null && dt.Rows.Count > 0)
{
DataView dv = dt.DefaultView;
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dv;
pds.AllowPaging = true;
pds.PageSize = 3;
pds.CurrentPageIndex = int.Parse(ViewState["pageindex"].ToString());
if (!pds.IsFirstPage)
{
lbPrevious.Visible = true;
}
else
{
lbPrevious.Visible = false;
}
if (!pds.IsLastPage)
{
lbNext.Visible = true;
}
else
{
lbNext.Visible = false;
}
dlProduct.DataSource = pds;
dlProduct.DataBind();
lbCurrent.Text = (pds.CurrentPageIndex + 1).ToString();
lbTotal.Text = pds.PageCount.ToString();
}
}
protected void IndexChange(object sender, CommandEventArgs e)
{
string cmd = ((LinkButton)sender).CommandArgument.ToString();
int pageindex = int.Parse(ViewState["pageindex"].ToString());
if (cmd == "previous")
{
pageindex += -1;
}
else if (cmd == "next")
{
pageindex += 1;
}
ViewState["pageindex"] = pageindex;
BindData();
}
protected void dlProduct_ItemCommand(object sender, DataListCommandEventArgs e)
{
//Int64 pid = Convert.ToInt64(e);
//string sqlDeleteProduct = "delete from Tb_product where Pid=" + pid;
string cmd = e.CommandName;
Int64 pid = Convert.ToInt64(e.CommandArgument);
if (cmd == "delete")
{
this.ClientScript.RegisterStartupScript(GetType(),"","<script>alert('"+pid.ToString()+"')</script>");
}
}
}
}