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

📄 productlist.ascx.cs

📁 这是一个基于asp.net的网上气球店
💻 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;

public partial class UserControl_ProductList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PopulateControls();
    }
    private void PopulateControls() 
    {
        string departmentId=Request.QueryString["DepartmentID"];
        string categoryId=Request.QueryString["CategoryID"];
        string page=Request.QueryString["Page"];
        string searchString=Request.QueryString["Search"];
        if (page == null) 
        {
            page = "1";
        }
        int howManyPages = 1;
        if(searchString!=null)
        {
            string allWords=Request.QueryString["AllWords"];
            list.DataSource = CatalogAccess.Search(searchString,allWords,page,out howManyPages);
            list.DataBind();
        }
        else if(categoryId!=null)//如果点击了分类将显示该分类下的所有商品
        {
            list.DataSource=CatalogAccess.GetProductsInCategory(categoryId,page,out howManyPages);
            list.DataBind();
        }
        else if(departmentId!=null)//如果点击了门类将显示该门类下的特色商品
        {
            list.DataSource = CatalogAccess.GetProductsOnDepartmentPromotion(departmentId, page, out howManyPages);
            list.DataBind();
        }
        else //意味着什么都没有被点击,顾客刚进入主页,显示一些在主页下显示的商品
        {
            list.DataSource = CatalogAccess.GetProductsOnCatalogPromotion(page,out howManyPages);
            list.DataBind();
        }
        //如果返回的页数大于1,需要分页显示
        if(howManyPages>1)
        {
            int currentPage = Int32.Parse(page);
            PagingLabel.Visible = true;
            previousLink.Visible = true;
            nextLink.Visible = true;
            PagingLabel.Text = "Page " + page + " of " + howManyPages.ToString();
            //如果是第一页,"上一页"不启用(解决"上一页"问题)
            if (currentPage == 1)
            {
                previousLink.Enabled = false;
            }
            else 
            {
               System.Collections.Specialized.NameValueCollection query=Request.QueryString;
               string paraName, newQueryString = "?";
               for (int i = 0; i < query.Count;i++ )
               {
                   if(query.AllKeys[i]!=null)
                   {
                    if((paraName=query.AllKeys[i].ToString()).ToUpper()!="PAGE")
                    {
                        newQueryString += paraName + "=" + query[i] + "&";
                    }
                    previousLink.NavigateUrl = Request.Url.AbsolutePath + newQueryString + "Page=" + (currentPage - 1).ToString();
                   
                   }
               }
            }
            //解决"下一页"问题(方案时找出所有?参数,)
            if(currentPage==howManyPages)
            {
                nextLink.Enabled = false;
            }
            else
            {
                System.Collections.Specialized.NameValueCollection query = Request.QueryString;
                string paraName, newQueryString = "?";
                for (int i = 0; i < query.Count; i++)
                {
                    if (query.AllKeys[i] != null)
                    {
                        if ((paraName = query.AllKeys[i].ToString()).ToUpper() != "PAGE")
                        {
                            newQueryString += paraName + "=" + query[i] + "&";
                        }
                       nextLink.NavigateUrl = Request.Url.AbsolutePath + newQueryString + "Page=" + (currentPage +1).ToString();
                        
                    }
                }
            }
        }
    }
    
    protected void list_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string productId = e.CommandArgument.ToString();
        ShoppingCartAccess.AddItem(productId);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -