0
我正在使用ASP.NET Webforms。我正在创建基于其属性过滤产品的功能。产品过滤器,复选框循环 - 加速过程
我现在这样做的方式花了太长时间。我需要更好,更快捷的方式。
我现在的方法如下。我有一个显示属性名称的GridView。 CheckBoxList嵌套在包含属性值的ItemTemplate中。 CheckBoxList具有autopostback =“true”。在每个selectedindexchanged事件中,我将通过复选框循环并根据选中的状态进行过滤。然后我比较过滤结果和复选框值,并禁用不在过滤结果中的复选框。
下面是该做的是,代码:
protected void cblAttr_SelectedIndexChanged(object sender, EventArgs e)
{
var filteredResults = ProductAttribute.Get(Convert.ToInt32(Request.QueryString["idsite"]));
var selectedAttributes = filteredResults;
foreach (GridViewRow row in gridAttrSet.Rows)
{
foreach (ListItem item in ((CheckBoxList)row.FindControl("cblAttr")).Items)
{
if (item.Selected == true)
{
// filter
selectedAttributes = selectedAttributes.Where(a => a.idAttr == Convert.ToInt32(item.Value)).ToList();
}
}
}
// this will now contain
filteredResults = (from c in filteredResults
join o in selectedAttributes
on c.idProduct equals o.idProduct
select new ProductAttribute
{
idProduct = c.idProduct,
idAttrSet = c.idAttrSet,
idAttr = c.idAttr,
}).ToList();
foreach (GridViewRow row in gridAttrSet.Rows)
{
if (row.RowIndex > ((GridViewRow)((CheckBoxList)sender).NamingContainer).RowIndex)
{
foreach (ListItem item in ((CheckBoxList)row.FindControl("cblAttr")).Items)
{
// disable if filtered out
item.Enabled = !(filteredResults.Where(a => a.idAttr == Convert.ToInt32(item.Value)).ToList().Count == 0);
}
}
}
}
正如你可以看到,有很多的循环发生的情况,并可以有上千条记录。
什么是更快的方法来做到这一点?任何想法,将不胜感激。
谢谢!
最好的选择是使用一些客户端功能来实现它..它会比代码端事件更快地丢失 –
我不知道这会节省多少时间,但你应该能够用LINQ语句替换第二组嵌套循环,以返回需要禁用的项目。事实上,你可以用两套嵌套循环来做到这一点。 –
另外,你是否证实过滤是你失去时间的地方,而不是建立GridView HTML? –