【一】建立页面:newpro.aspx
(1):把数据库中新品上市的商品显示到页面中:
aspx代码:
<table width="100%" border="0">
<tr>
<td height="35" bgcolor="#CCCCCC" style="font-weight: bold; padding-left: 10px;">
<img src="images/dot_03.gif" width="9" height="9" />
新品上架
</td>
</tr>
<tr>
<td height="35" align="right" style="padding-right: 20px; border-bottom: 1px #999999 solid;">
选择查看方式:
<select name="select">
<option>按商品关注度排序</option>
</select>
</td>
</tr>
<tr>
<td>
<asp:Repeater ID="rep" runat="server">
<ItemTemplate>
<div class="proitem">
<div class="proitem_img">
<a href='pro.aspx?id=<%#Eval("id") %>' title='<%#Eval("proname") %>'>
<img src='upload/<%#Eval("proimg") %>' width="85" height="105" /></a>
</div>
<div class="proitem_right">
<div>
市场价:<span class="delfont">¥<%#Eval("marketprice") %></span></div>
<div>
会员价:<span class="redfont">¥<%#Eval("memberprice") %></span></div>
<div>
<a href="#">
<img src="images/gm.gif" /></a></div>
<div>
<a href="#">
<img src="images/shc.gif" /></a></div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
<tr>
<td align="center">
<webdiyer:AspNetPager ID="anp" runat="server" FirstPageText="首页"
LastPageText="尾页" NextPageText="下一页" NumericButtonCount="5"
PrevPageText="上一页"
CustomInfoHTML="共%PageCount%页,当前为第%CurrentPageIndex%页,每页%PageSize%条"
PageSize="5" ShowCustomInfoSection="Left" AlwaysShow="true" onpagechanged="anp_PageChanged">
</webdiyer:AspNetPager>
</td>
</tr>
</table>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web
{
public partial class newpro : System.Web.UI.Page
{
MyShop.DAL.ProductDAO dao = new MyShop.DAL.ProductDAO();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
anp.RecordCount = dao.CalcCount(GetCond());
BindRep();
}
}
//绑定数据
private void BindRep()
{
rep.DataSource = dao.GetList("*", "createDate", "desc", anp.PageSize, anp.CurrentPageIndex, GetCond());
rep.DataBind();
}
//获取条件
private string GetCond()
{
string cond = "isxp=1";
return cond;
}
//分页
protected void anp_PageChanged(object sender, EventArgs e)
{
BindRep();
}
}
}
aspx代码:
注意添加: AutoPostBack="true"
<tr>
<td height="35" align="right" style="padding-right: 20px; border-bottom: 1px #999999 solid;">
选择查看方式:
<asp:DropDownList ID="ddlorder" runat="server">
<asp:ListItem Text="↓按上架时间降序排列" Value="0"></asp:ListItem>
<asp:ListItem Text="↑按上架时间升序排列" Value="1"></asp:ListItem>
<asp:ListItem Text="↓按商品会员价格降序排列" Value="2"></asp:ListItem>
<asp:ListItem Text="↑按商品会员价格升序排列" Value="3"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web
{
public partial class newpro : System.Web.UI.Page
{
MyShop.DAL.ProductDAO dao = new MyShop.DAL.ProductDAO();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
anp.RecordCount = dao.CalcCount(GetCond());
BindRep();
}
}
//绑定数据
private void BindRep()
{
string order = "createDate";
string ordertype = "desc";
switch (ddlorder.SelectedValue)
{
case "0":
order = "createDate";
ordertype = "desc";
break;
case "1":
order = "createDate";
ordertype = "asc";
break;
case "2":
order = "memberprice";
ordertype = "desc";
break;
case "3":
order = "memberprice";
ordertype = "asc";
break;
default:
break;
}
rep.DataSource = dao.GetList("*", order, ordertype, anp.PageSize, anp.CurrentPageIndex, GetCond());
rep.DataBind();
}
//获取条件
private string GetCond()
{
string cond = "isxp=1";
return cond;
}
//分页
protected void anp_PageChanged(object sender, EventArgs e)
{
BindRep();
}
//选择排序方式
protected void ddlorder_SelectedIndexChanged(object sender, EventArgs e)
{
BindRep();
}
}
}
(3):实现加入收藏按钮【结合购物网第三阶段总结笔记6】
aspx代码:
<asp:LinkButton ID="LinkButton1" OnClick="Favorite" CommandArgument='<%#Eval("id") %>' runat="server">
<img src="images/shc.gif" />
</asp:LinkButton>
cs代码:
//加入收藏
protected void Favorite(object sender, EventArgs e)
{
string proid = (sender as LinkButton).CommandArgument;
MyShop.DAL.FavoriteDAO dao = new MyShop.DAL.FavoriteDAO();
int y = dao.CalcCount("username='"+User.Identity.Name+"' and proid="+proid);
if (y!=0)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('该商品您已经收藏过!')</script>");
return;
}
int x = dao.CalcCount("username='"+User.Identity.Name+"'");
if (x==10)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('您的收藏夹已满!')</script>");
return;
}
else
{
dao.Add(new MyShop.Model.Favorite() {
proid=int.Parse(proid),
createDate=DateTime.Now,
username=User.Identity.Name
});
Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('收藏商品成功!')</script>");
}
}
【二】:特价商品页面:tejiapro.aspx,和新品上架一摸一样,这部分视频不用看,笔记省略。