⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 productlist.aspx.cs

📁 B2B网上商城
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
using System.Configuration;

public partial class productList : System.Web.UI.Page
{   
    //定义变量来保存总页数
    public static int TotalPage;
    //定义变量来保存当前页索引
    public int CurPage;
    //总条数;
    public int Tnum;
    //每页总条数
    public int EachPage;

    public SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
    public String strDaohang = "";
    public String str1 = "";
    public DataTable myTable = new DataTable("myTable");
    public DataTable myTable2 = new DataTable("myTable2");
    protected void Page_Load(object sender, 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(ConfigurationManager.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 += " &gt;&gt; ";
            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(ConfigurationManager.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(ConfigurationManager.AppSettings["ConnStr"]);
        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;
        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);
        Repeater2.DataSource = objPage;
        Repeater2.DataBind();
        conn.Close();
    }


    protected void btnPage_Click(object sender, 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 + -