最近做了个类似京东商城筛选模块的功能,在网上找了些资料,再加了些自己的思路做了这样一个功能,在这里分享一下,主要针对新手,希望大家共同探讨
前台是两个repeater嵌套
<asp:Repeater ID="rptList" runat="server" onitemdatabound="rptList_ItemDataBound">
<ItemTemplate>
<tr>
<td><%# Eval("PropertyName")%></td>
<td><asp:Literal ID="litPropertyValueAll" runat="server" /></td>
<td>
<asp:Repeater ID="rptValue" runat="server" OnItemDataBound="rptValue_ItemDataBound">
<ItemTemplate> <asp:Label ID="lblHidden" runat="server" Visible="false" />
<span style="color:#ccc;">|</span> <asp:Literal ID="litPropertyValue" runat="server" />
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
后台
View Code
/// <summary>
/// 属性数据绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rpt = (Repeater)e.Item.FindControl("rptValue");
int id = ((ProductCategorySearchPropertyModel)e.Item.DataItem).ID;
rpt.DataSource = Product_Category_SearchProperty_ValueBLL.GetPropertyValue(id);
rpt.DataBind();
#region 所有属性
//所有条件
string allProperty = "";
foreach (RepeaterItem item in rpt.Items)
{
Label lab = (Label)item.FindControl("lblHidden");
allProperty = lab.Text;
}
Literal litPropertyValue = (Literal)e.Item.FindControl("litPropertyValueAll");
litPropertyValue.Text = allProperty;
#endregion
}
}
/// <summary>
/// 属性值数据绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void rptValue_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
#region 解析Url属性参数
//解析Url传的属性参数
if (!string.IsNullOrEmpty(searchPropertyIDReq))
{
ArrayList tempSearchPropertyValueIDArrayList = new ArrayList();
string[] tempSearchPropertyValueIDArray = searchPropertyIDReq.Split('-');
foreach (string item in tempSearchPropertyValueIDArray)
{
if (string.IsNullOrEmpty(item))
{
continue;
}
try
{
Convert.ToInt32(item);
}
catch
{
continue;
}
tempSearchPropertyValueIDArrayList.Add(Convert.ToInt32(item));
}
searchPropertyValueIDArray = (int[])tempSearchPropertyValueIDArrayList.ToArray(typeof(int));
}
#endregion
#region 绑定商品列表
this.rptProductList.DataSource = ProductBLL.SearchProduct(searchPropertyValueIDArray);
this.rptProductList.DataBind();
#endregion
#region rptValue 数据绑定
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string curPropertyName = DataBinder.Eval(e.Item.DataItem, "PropertyName").ToString();
string curPropertyValue = DataBinder.Eval(e.Item.DataItem, "PropertyValue").ToString();
//当前RUL
string curPropertyValueID = DataBinder.Eval(e.Item.DataItem, "ID").ToString();
string cNo = DataBinder.Eval(e.Item.DataItem, "cNo").ToString();
bool isHasCurrentPropertyName = false;
int? urlPropertyValueID = null;
if (searchPropertyValueIDArray != null)
{
foreach (int searchPropertyValueID in searchPropertyValueIDArray)
{
ProductCategorySearchPropertyValueModel properValue = Product_Category_SearchProperty_ValueBLL.GetPropertyValueA(searchPropertyValueID);
int searchPropertyID = properValue.SearchPropertyID;
ProductCategorySearchPropertyModel searchProperty = Product_Category_SearchPropertyBLL.GetSearchPropertyA(searchPropertyID);
if (searchProperty.PropertyName == curPropertyName)
{
urlPropertyValueID = searchPropertyValueID;
isHasCurrentPropertyName = true;
break;
}
}
}
//原始URL
string searchPropertyIDUrl = "";
if (searchPropertyValueIDArray != null)
{
foreach (int searchPropertyValueID in searchPropertyValueIDArray)
{
if (searchPropertyValueID != urlPropertyValueID)
{
searchPropertyIDUrl += "-" + searchPropertyValueID.ToString();
}
}
}
//具体属性
Literal litPropertyValue = (Literal)e.Item.FindControl("litPropertyValue");
Label hiddenValue = (Label)e.Item.FindControl("lblHidden");
if (isHasCurrentPropertyName)
{
if (urlPropertyValueID != null && urlPropertyValueID.ToString() == curPropertyValueID)
{
litPropertyValue.Text = "<a href=\"" + cNo + searchPropertyIDUrl + "-" + curPropertyValueID + ".aspx\" class=\"curSearchProperty\" isCurrent=\"true\">" + curPropertyValue + "</a>";
}
else
{
litPropertyValue.Text = "<a href=\"" + cNo + searchPropertyIDUrl + "-" + curPropertyValueID + ".aspx\" class=\"normal\">" + curPropertyValue + "</a>";
}
}
else
{
litPropertyValue.Text = "<a href=\"" + cNo + searchPropertyIDUrl + "-" + curPropertyValueID + ".aspx\" class=\"normal\">" + curPropertyValue + "</a>";
}
hiddenValue.Text = "<a href=\"" + cNo + searchPropertyIDUrl + ".aspx\" class=\"allProperty\" isCurrent=\"true\">全部</a>";
}
#endregion
}
这样的基本效果就实现了