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

2018-06-17 21:37:30来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

分页控件为: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", "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>");
    //    }
    //}
    //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;
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C#开发中使用配置文件对象简化配置的本地保存

下一篇:基本的Web控件一