private void MyBind()
{
int currentpage = string.IsNullOrEmpty(Request["page"]) ? 1 : Convert.ToInt32(Request["page"]);
PageBean pg = Product.SelectByPage(currentpage, 3);
DataList1.DataSource = pg.Data;
DataList1.DataBind();
//传统分页工具栏
Label5.Text = pg.Sum + "";
Label6.Text = pg.Pagesum + "";
Label7.Text = pg.Currentpage + "";
if (pg.Currentpage == 1)
{
LinkButton1.Visible = false;
LinkButton2.Visible = false;
}
if (pg.Currentpage == pg.Pagesum)
{
LinkButton3.Visible = false;
LinkButton4.Visible = false;
}
//数值型分页
for (int i = 1; i <= pg.Pagesum; i++)
{
if (i == pg.Currentpage)
{
Label x = new Label();
x.Text = i + "";
x.Width = 22;
Panel1.Controls.Add(x);
}
else
{
HyperLink h = new HyperLink();
h.Text = i + "";
h.NavigateUrl = "Default.aspx?page=" + i;
h.Width = 22;
Panel1.Controls.Add(h);
}
}
//阶段性分页
int n=5;//每页的页码个数
int first=(pg.Currentpage-1)/n*n+1;//pg.currenpage
int end=(first+n-1)>pg.Pagesum?pg.Pagesum:(first+n-1);
//上n页
if (pg.Currentpage > n)
{
HyperLink hfirst = new HyperLink();
hfirst.Text = "上" + n + "页";
hfirst.NavigateUrl = "Default.aspx?page=" + (first - 1);
Panel2.Controls.Add(hfirst);
}
//数码
for (int i = first; i <= end; i++)
{
if (i == pg.Currentpage)
{
Label x = new Label();
x.Text = i + "";
x.Width = 22;
Panel2.Controls.Add(x);
}
else
{
HyperLink h = new HyperLink();
h.Text = i + "";
h.NavigateUrl = "Default.aspx?page=" + i;
h.Width = 22;
Panel2.Controls.Add(h);
}
}
//下n页
if (end < pg.Pagesum)
{
HyperLink hend = new HyperLink();
hend.Text = "下" + n + "页";
hend.NavigateUrl = "Default.aspx?page=" + (end + 1);
Panel2.Controls.Add(hend);
}
}
//----------------根据需要 赛选
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MyBind();
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx?page=1");
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
int currentpage = Convert.ToInt32(Label7.Text);
Response.Redirect("Default.aspx?page="+(currentpage+1));
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
int currentpage = Convert.ToInt32(Label7.Text);
Response.Redirect("Default.aspx?page=" + (currentpage -1));
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx?page=" + Convert.ToInt32(Label6.Text));
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
case "goumai":
Label8.Text = "你要购买";
break;
case "shoucang":
Label8.Text = "您要收藏";
break;
}
Label idLabel = (Label)e.Item.FindControl("Label1");
Label8.Text += idLabel.Text + "号商品";
Label priceLabel = (Label)e.Item.FindControl("Label4");
Label8.Text += "单价:"+priceLabel.Text;
TextBox numTextBox = (TextBox)e.Item.FindControl("TextBox1");
Label8.Text += ",购买量:" + numTextBox.Text;
}
protected void Button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < DataList1.Items.Count; i++)
{
DataListItem dli = DataList1.Items[i];
CheckBox c1 = (CheckBox)dli.FindControl("CheckBox1");
if (c1.Checked)
{
Label8.Text += "<br>您选择了:";
Label idLabel = (Label)dli.FindControl("Label1");
Label8.Text += idLabel.Text + "号商品";
Label priceLabel = (Label)dli.FindControl("Label4");
Label8.Text += "单价:"+priceLabel.Text;
}
}
}
|