📄 productlist.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.SqlClient;
using System.Configuration ;
namespace B2Bsite
{
/// <summary>
/// 商品列表
/// </summary>
public class productList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label daohang;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
//protected System.Web.UI.WebControls.DataGrid DataGrid2;
//protected System.Web.UI.WebControls.DataGrid DataGrid3;
protected System.Web.UI.WebControls.Repeater Repeater1;
protected System.Web.UI.WebControls.Repeater Repeater2;
protected System.Web.UI.WebControls.Label lblCurPage;
protected System.Web.UI.WebControls.Label lblEachPage;
protected System.Web.UI.WebControls.Label lblTnum;
protected System.Web.UI.WebControls.HyperLink lnkFirst;
protected System.Web.UI.WebControls.HyperLink lnkPrev;
protected System.Web.UI.WebControls.HyperLink lnkNext;
protected System.Web.UI.WebControls.HyperLink lnkLast;
protected System.Web.UI.WebControls.TextBox txtPage;
protected System.Web.UI.WebControls.Button btnPage;
public static int TotalPage;//定义变量来保存总页数
public int CurPage;//定义变量来保存当前页索引
public int Tnum;//总条数;
public int EachPage;//每页总条数
public SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
public String strDaohang ="";
public String str1="";
public DataTable myTable = new DataTable("myTable");
public DataTable myTable2 = new DataTable("myTable2");
private void Page_Load(object sender, System.EventArgs e)
{
if(Request.QueryString["categoryid"]!=null)
{
String strID = Request.QueryString["categoryid"].ToString ();
//绑定 类别所对应的子类
String sql="SELECT * FROM PType where parentId=" + strID;
SqlCommand command=new SqlCommand(sql,conn);
conn.Open();
SqlDataReader dr=command.ExecuteReader();
Repeater1.DataSource = dr;
Repeater1.DataBind();
dr.Close();
conn.Close();
//导航条
InitDaohang(strDaohang,strID);
daohang.Text =strDaohang;
//--------------------------------------
myTable.Columns.Add("category_ID");
myTable.Columns.Add("category_name");
String sql2="SELECT * FROM PType where category_ID=" + strID;
SqlCommand command2=new SqlCommand(sql2,conn);
conn.Open();
SqlDataReader dr2=command2.ExecuteReader();
if(dr2.Read())
{
DataRow row = myTable.NewRow();
row["category_ID"]=dr2["category_ID"].ToString();
row["category_name"]=dr2["category_name"].ToString();
myTable.Rows.Add(row);
}
dr2.Close();
conn.Close();
//----------------------------------
ClassList(strID);
//Response.Write(str1 + "<br>");
//DataGrid2.DataSource=myTable;
//DataGrid2.DataBind();
myTable2.Columns.Add("id");
myTable2.Columns.Add("CId");
myTable2.Columns.Add("name");
productsList();
}
}
private void InitDaohang(string strTmp,string parentId)//初始化导航条
{
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
String sql="SELECT * FROM PType where category_ID=" + parentId;
SqlCommand command=new SqlCommand(sql,conn);
conn.Open();
SqlDataReader dr=command.ExecuteReader();
if(dr.Read())
{
InitDaohang(strTmp,dr["parentId"].ToString());
strTmp += " >> ";
strTmp += "<a href ='productList.aspx?categoryid=";
strTmp += dr["category_ID"].ToString();
strTmp += "'>";
strTmp += dr["category_name"].ToString();
strTmp += "</a>";
strDaohang += strTmp;
}
dr.Close();
conn.Close();
}
private void ClassList(string CId)
{
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
String sql="SELECT * FROM PType where parentId=" + CId;
SqlCommand command=new SqlCommand(sql,conn);
conn.Open();
SqlDataReader dr=command.ExecuteReader();
string strTmp="";
while(dr.Read())
{
strTmp += " : ";
strTmp += "<a href ='productList.aspx?categoryid=";
strTmp += dr["category_ID"].ToString();
strTmp += "'>";
strTmp += dr["category_name"].ToString();
strTmp += "</a>";
ClassList(dr["category_ID"].ToString());
DataRow row = myTable.NewRow();
row["category_ID"]=dr["category_ID"].ToString();
row["category_name"]=dr["category_name"].ToString();
myTable.Rows.Add(row);
}
str1 += strTmp;
dr.Close();
conn.Close();
}
private void productsList()//商品列表
{
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
/*
String sql="SELECT * FROM Products where ProductType=0";
foreach(DataRow row in myTable.Rows)
{
sql += " or ProductType=" + row["category_ID"].ToString();
}
*/
String sql="Select * from Products where (ProductType=0";
foreach(DataRow row in myTable.Rows)
{
sql += " or ProductType=" + row["category_ID"].ToString();
}
sql += ") and ProductState=0 order by ProductId desc";
SqlDataAdapter da=new SqlDataAdapter(sql,conn);
DataSet ds=new DataSet();
da.Fill(ds,"Table");
PagedDataSource objPage=new PagedDataSource();//创建分页类
objPage.DataSource=ds.Tables["Table"].DefaultView;//设置数据源
/*
SqlCommand command=new SqlCommand(sql,conn);
conn.Open();
SqlDataReader dr=command.ExecuteReader();
*/
//PagedDataSource objPage=new PagedDataSource();
//objPage.DataSource=dr;
objPage.AllowPaging=true;
objPage.PageSize=5;
if(Request.QueryString["Page"]!=null)
{
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
CurPage=Math.Min(CurPage,objPage.PageCount);
CurPage=Math.Max(CurPage,1);
}
else
CurPage=1;
objPage.CurrentPageIndex=CurPage-1;
TotalPage=objPage.PageCount;
Tnum=objPage.DataSourceCount;
EachPage=objPage.Count;
lblCurPage.Text="第 "+CurPage.ToString()+" / "+TotalPage.ToString()+" 页";
lblTnum.Text="共: " +Tnum+" 条记录";
lblEachPage.Text="每页有: "+EachPage.ToString()+" 条记录";
if(objPage.CurrentPageIndex!=0)
lnkFirst.NavigateUrl=Request.CurrentExecutionFilePath+"?categoryid="+Request.QueryString["categoryid"].ToString()+"&Page="+Convert.ToString(1);
if(objPage.CurrentPageIndex!=TotalPage-1)
lnkLast.NavigateUrl=Request.CurrentExecutionFilePath+"?categoryid="+Request.QueryString["categoryid"].ToString()+"&Page="+Convert.ToString(TotalPage);
if(!objPage.IsFirstPage)
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath+"?categoryid="+Request.QueryString["categoryid"].ToString()+"&Page="+Convert.ToString(CurPage-1);
if(!objPage.IsLastPage)
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+"?categoryid="+Request.QueryString["categoryid"].ToString()+"&Page="+Convert.ToString(CurPage+1);
//DataGrid3.DataSource=dr;
//DataGrid3.DataBind();
Repeater2.DataSource = objPage;
Repeater2.DataBind();
//dr.Close();
conn.Close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnPage.Click += new System.EventHandler(this.btnPage_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnPage_Click(object sender, System.EventArgs e)
{
int PageNum=0;
if( !Request.Form["txtPage"].Equals("") )
PageNum=Convert.ToInt32(Request.Form["txtPage"]);
if(PageNum<=0 || PageNum>TotalPage)
Response.Redirect(Request.CurrentExecutionFilePath+"?categoryid="+Request.QueryString["categoryid"].ToString()+"&Page="+Convert.ToString(1));
else
Response.Redirect(Request.CurrentExecutionFilePath+"?categoryid="+Request.QueryString["categoryid"].ToString()+"&Page="+Convert.ToString(PageNum));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -