📄 category.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace MobileShop
{
/// <summary>
/// Category 的摘要说明。
/// </summary>
public class Category : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lbCurPage;
protected System.Web.UI.WebControls.HyperLink lnkPrev;
protected System.Web.UI.WebControls.DataList dlShowProduct;
protected System.Web.UI.WebControls.HyperLink lnkNext;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
DataListDataBind();
}
}
private void DataListDataBind()
{
string ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
ConnectionString += Server.MapPath("MobileShop.mdb");
OleDbConnection conn = new OleDbConnection(ConnectionString);
string SqlString = "select ProductID,UnitCost,Introduce,Picture from Product where Manufacturer='";
SqlString += Request.QueryString["category_id"] + "'";
OleDbDataAdapter da = new OleDbDataAdapter(SqlString, conn);
DataSet ds = new DataSet();
conn.Open();
try
{
da.Fill(ds, "Product");
//创建分页类
PagedDataSource objPage = new PagedDataSource();
//设置数据源
objPage.DataSource = ds.Tables["Product"].DefaultView;
//允许分页
objPage.AllowPaging = true;
//设置每页显示的项数
objPage.PageSize = 5;
//定义变量来保存当前索引
int CurPage;
//判断是否具有页面跳转的请求
if(Request.QueryString["Page"]!=null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;
//设置当前页的索引
objPage.CurrentPageIndex = CurPage-1;
//显示状态信息
lbCurPage.Text = "当前页:第" + CurPage.ToString() + "页";
//如果当前页面不是首页
if(!objPage.IsFirstPage)
//定义“上一页”超级链接的URL为:当前执行页面的虚拟路径,并传递下一页面的索引
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);
//如果当前页面不是最后一页
if(!objPage.IsLastPage)
//定义“下一页”超级链接的URL为:当前执行页面的虚拟路径,并传递下一页面的索引值
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage +1);
dlShowProduct.DataSource = objPage;
dlShowProduct.DataBind();
}
catch(Exception err)
{
Response.Write( err.Message );
}
finally
{
conn.Close();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dlShowProduct.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlShowProduct_ItemCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dlShowProduct_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
Response.Cookies["BuyCart"]["ProductID"] = e.Item.ItemIndex.ToString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -