• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NET动态属性筛选和分页绑定

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

分页控件为:AspNetPager.dll

我们先建立一个产品属性名称表

CREATE TABLE ProductAttr
(
    [ID] [int] IDENTITY(1,1) NOT NULL primary key,
    [ParentID] [int] NULL,
    [Kind] [int] NULL,
    [CNName] [nvarchar](50) NOT NULL,
    [ENName] [nvarchar](50) NOT NULL,
    [Sort] [int] NOT NULL,
    [AddTime] [datetime],
)

 

再建一个属性内容表

CREATE TABLE ProductAttrInfo
(
    [ID] [int] IDENTITY(1,1) NOT NULL primary key,
    [ParentID] [int] NULL,
    [CNName] [nvarchar](50) NOT NULL,
    [Sort] [int] NOT NULL,
    [AddTime] [datetime] NOT NULL,
    [Color] [nvarchar](50) NULL,
)

 

产品表叫Product

页面呈现效果为:

 

其中的产品分类、形状、其他为动态的产品属性名称,对象相关的属性内容。

 

我们首页要把相关的内容绑定,然后再根据传参,把相关的属性编号和内容编号,通过正则的方式找出来,然后把当前也是和属性内容的ID找出来,方便我们查询产品。

 

/// <summary>
/// 通过正则,把相关的属性ID拿到,然后判断属性名称ID的值即为属性值的ID值
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
protected int getAttrSql(int ids)
{
    int value = 0;
    string url = Request.Url.ToString().ToLower();
    if (url.IndexOf("?") >= 0)
    {
        string index = url.Remove(0, url.IndexOf("?") + 1);
        string para = index.Replace("?", "");
        //切割成一个一个的参数并转换成SQL语句
        string[] ps = para.Split('&');
        string sql = "";
        for (int i = 0; i < ps.Length; i++)
        {
            string s = ps[i];
            //检查参数是否是p\d*=\d*开头的,如果是则认为这个参数是属性的参数
            if (System.Text.RegularExpressions.Regex.IsMatch(s, @"p\d{1,}=\d{1,}"))
            {
                //去掉p切割成数字
                string[] pp = s.Replace("p", "").Split('=');
                int c = int.Parse(pp[0].ToString());
                if (c == ids)
                {
                    value = int.Parse(pp[1].ToString());
                    break;
                }
            }
        }
    }
    return value;
}
/// <summary>
/// 绑定相关属性名称和内容,其中参数one和two,我这里是一级二级产品编号,可以忽略,ParentID为一级产品分类的ID,Kind为二级产品分类的ID
/// </summary>
/// <param name="one"></param>
/// <param name="two"></param>
public void BindAttrInfo(Model.ProductType one, Model.ProductType two)
{
    StringBuilder sb = new StringBuilder();
    //if (two != null)
    //{
    //    List<Model.ProductAttr> list = new BLL.ProductAttr().GetList(0, "CNName,ID", "ParentID=" + one.ID + " and Kind=" + two.ID + "", "Sort desc,AddTime desc");
    //    foreach (Model.ProductAttr l in list)
    //    {
    //        sb.AppendFormat("<dl class=\"clearfix\"><dt>{0}</dt>", l.CNName);
    //        List<Model.ProductAttrInfo> list2 = new BLL.ProductAttrInfo().GetList(0, "CNName,ID", "P);
    //        if (list2.Count > 0)
    //        {
    //            sb.AppendFormat("<dd><a href=\"{0}\" {1}>全部</a>", getUrl("p" + l.ID.Value, ""), getAttrSql(l.ID.Value) <= 0 ? "class=\"cur\"" : "");
    //            foreach (Model.ProductAttrInfo l2 in list2)
    //            {
    //                sb.AppendFormat("<a href=\"{0}\" {1}>{2}</a>", getUrl("p" + l.ID.Value, l2.ID.Value.ToString()), getAttrSql(l.ID.Value) == l2.ID.Value ? "class=\"cur\"" : "", l2.CNName);
    //            }
    //            sb.AppendFormat("</dd>");
    //        }
    //        sb.Append("</dl>");
    //    }
    //}
    //else
    //{
    List<Model.ProductType> twoType = new BLL.ProductType().GetList(0, "CNName,ID", "IsPass=1 and Kind=" + one.ID + "", "Sort desc,AddTime desc");
    if (twoType.Count > 0)
    {
        sb.AppendFormat("<dl class=\"clearfix\"><dt>产品分类</dt><dd>");
        sb.AppendFormat("<a {1} href=\"/products/?ParentID={0}\">全部</a>", one.ID, Kind <= 0 ? "class=\"cur\"" : "");
        foreach (Model.ProductType l in twoType)
        {
            sb.AppendFormat("<a href=\"/products/?ParentID={0}&Kind={1}\" {3}>{2}</a>", one.ID.Value, l.ID.Value, l.CNName, Kind == l.ID.Value ? "class=\"cur\"" : "");
        }
        sb.AppendFormat("</dd></dl>");
    }
    List<Model.ProductAttr> list = new BLL.ProductAttr().GetList(0, "CNName,ID", "ParentID=" + one.ID + " and Kind=0", "Sort desc,AddTime desc");
    foreach (Model.ProductAttr l in list)
    {
        sb.AppendFormat("<dl class=\"clearfix\"><dt>{0}</dt>", l.CNName);
        List<Model.ProductAttrInfo> list2 = new BLL.ProductAttrInfo().GetList(0, "CNName,ID", "Pid=" + l.ID + "", "Sort desc,AddTime desc");
        if (list2.Count > 0)
        {
            sb.AppendFormat("<dd><a href=\"{0}\" {1}>全部</a>", getUrl("p" + l.ID.Value, ""), getAttrSql(l.ID.Value) <= 0 ? "class=\"cur\"" : "");
            foreach (Model.ProductAttrInfo l2 in list2)
            {
                sb.AppendFormat("<a href=\"{0}\" {1}>{2}</a>", getUrl("p" + l.ID.Value, l2.ID.Value.ToString()), getAttrSql(l.ID.Value) == l2.ID.Value ? "class=\"cur\"" : "", l2.CNName);
            }
            sb.AppendFormat("</dd>");
        }
        sb.Append("</dl>");
    }
    // }
    Lit_ProductAttrList.Text = sb.ToString();
}
/// <summary>
/// 绑定产品
/// </summary>
public void BindProducts()
{
    string where = "ParentID=" + ParentID + "";
    if (Kind > 0)
    {
        where += " and Kind=" + Kind + "";

        where += GetWhereAttr();
    }
    else
    {
where += GetWhereAttr(); } string strOrder = "Sort desc,AddTime desc"; if (OrderBy == 1) { strOrder = "SaleNum desc,Sort desc,AddTime desc"; } else if (OrderBy == 2) { strOrder = "SalPrice,Sort desc,AddTime desc"; } Model.PageData<Model.Product> data = new BLL.Product().GetList(20, base.PageIndex, "", where, strOrder); repList.DataSource = data.DataSoure; repList.DataBind(); pgServer.RecordCount = data.Count; pgServer.UrlRewritePattern = getUrl("1", "1").Replace("#pro-list", "") + "&page={0}#pro-list"; pgServer.PageSize = 20; AllCount = data.Count; AllPage = data.PageCount; ThisPage = base.PageIndex; if (ThisPage < 1) { ThisPage = 1; } if (AllPage == 0) { ThisPage = 0; } pgServer.DataBind(); }
/// <summary>
/// 获取Url
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
protected string getUrl(string name, string value)
{
    string url = System.Text.RegularExpressions.Regex.Replace(Request.Url.Query.Trim(), @"^\?", "");
    url = System.Text.RegularExpressions.Regex.Replace(url, "^" + name + "=.*?&|" + name + "=.*?&", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    url = System.Text.RegularExpressions.Regex.Replace(url, "&{0,1}" + name + "=.*", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    url = System.Text.RegularExpressions.Regex.Replace(url, "^page=.*?&|page=.*?&", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    url = System.Text.RegularExpressions.Regex.Replace(url, "&{0,1}page=.*", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    if (url.Trim() == "")
    {
        return "/products/?" + name + "=" + value + "#pro-list";
    }
    return getReplaceUrl("/products/?" + name + "=" + value + "&" + url.Replace("#pro-list", "") + "#pro-list", "key");
}
/// <summary>
/// 获取Url
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
protected string getReplaceUrl(string url, string name)
{
    url = System.Text.RegularExpressions.Regex.Replace(url, "^" + name + "=.*?&|" + name + "=.*?&", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    url = System.Text.RegularExpressions.Regex.Replace(url, "^page=.*?&|page=.*?&", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    url = System.Text.RegularExpressions.Regex.Replace(url, "&{0,1}page=.*", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);


    return url;
}
/// <summary>
/// 获取属性条件-绑定
/// </summary>
/// <returns></returns>
protected string GetWhereAttr(string where)
{
    string value = "";
    string url = Request.Url.ToString().ToLower();
    if (url.IndexOf("?") >= 0)
    {
        string index = url.Remove(0, url.IndexOf("?") + 1);
        string para = index.Replace("?", "");
        //切割成一个一个的参数并转换成SQL语句
        string[] ps = para.Split('&');
        string sql = "";
        for (int i = 0; i < ps.Length; i++)
        {
            string s = ps[i];
            //检查参数是否是p\d*=\d*开头的,如果是则认为这个参数是属性的参数
            if (System.Text.RegularExpressions.Regex.IsMatch(s, @"p\d{1,}=\d{1,}"))
            {
                //去掉p切割成数字
                string[] pp = s.Replace("p", "").Split('=');
                int c = int.Parse(pp[1].ToString());
                //c为属性内容的编号,根据程序自己做判断
            }
        }
    }
    return value;
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.netC#光标离开事件发布时间:2022-07-10
下一篇:
ASP.NET 4.5.256 has not been registered on the Web server. You need to manually ...发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap