网页端程序小知识
一、LinQ Distinct某字段去重
新建类GoodsIdComparer,继承 IEqualityComparer<Goods>,实现Equals方法
public class GoodsIdComparer : IEqualityComparer<Goods> { public bool Equals(Goods x, Goods y) { if (x == null) return y == null; return x.Gproducer == y.Gproducer; } public int GetHashCode(Goods obj) { if (obj == null) return 0; return obj.Gproducer.GetHashCode(); } }//根据产地(Gproducer)去重
使用的时候,只需要
var distinctGoods= allGoods.Distinct(new GoodsIdComparer());//需要引用命名空间
二、MVC的控制器Controllers中用using直接调用数据库组合查询
在MVC的控制器Controllers中用using直接调用数据库组合查询,返回视图时应注意为:
return View(new List<Goods>(All));
using (FruitDataContext con = new FruitDataContext()) { var All = con.Goods.AsEnumerable(); if (category != "") { var Category = con.Goods.Where(r => r.Gcategory == category); All = All.Intersect(Category); } } return View(new List<Goods>(All));
视图中引用强类型:
@model List<Goods>
三、LinQ查询数据库中自增列ID的最大值
public int Maxid() { return con.Goods.Max(r=>r.Gids); }
四、MARQUEE无缝滚动
<MARQUEE behavior="scroll" contenteditable="true" onstart="this.firstChild.innerHTML+=this.firstChild.innerHTML;" scrollamount="3" width="100"> <SPAN unselectable="on"></SPAN> </MARQUEE> <DIV id="scrollobj" style="white-space:nowrap;overflow:hidden;width:500px;" onmouseover="aa()" onmouseout="b()" >
//滚动的内容 [CROSSSELLING3] [CROSSSELLING4] [CROSSSELLING5] [CROSSSELLING6] [CROSSSELLING7] [CROSSSELLING8] [CROSSSELLING9] [CROSSSELLING10] </DIV> <script language="javascript" type="text/javascript"> function scroll(obj) { var tmp = (obj.scrollLeft)++; //当滚动条到达右边顶端时 if (obj.scrollLeft == tmp) obj.innerHTML += obj.innerHTML; //当滚动条滚动了初始内容的宽度时滚动条回到最左端 if (obj.scrollLeft >= obj.firstChild.offsetWidth) obj.scrollLeft = 0; } var a = setInterval("scroll(document.getElementById('scrollobj'))", 20); function aa() { clearInterval(a); } function b() { a = setInterval("scroll(document.getElementById('scrollobj'))", 10); } </script>
五、返回顶部小按钮
在网页开头写一个带id的<a></a>,然后,在任意位置写另一个样式是position:fixed;的<a></a>,连接到开头的<a></a>即可。
<body> <a id="_top"></a> <a href="#_top"> <div style="position:fixed;width:60px;height:60px;top:80%;right:0px; background-color:#808080;color:white;z-index:3;text-align:center;
padding-top:20px;">
返回<br /> 顶部
</div> </a> </body>
六、瀑布流布局
前台界面上:
首先一个大的DIV把所有内容包住,然后在里面并排float出相应列数的几个DIV,然后在每个DIV里放一个Repeater控件,Repeater控件的<ItemTemplate></ItemTemplate>项里面再放置需要float成一列的DIV,这样界面上的布局就完成了。
后台数据:
首先,定义相应列数的集合,然后,利用for循环遍历出数据库中的所有数据,按照一定的规则,利用if判断,把满足相应条件的每条数据添加到相对应的集合中。循环结束后,把相应的集合绑定到相对应的Repeater上即可。
using (sanshengyuanDataContext con = new sanshengyuanDataContext()) { List<HappinessList> lii1 = new List<HappinessList>(); List<HappinessList> lii2 = new List<HappinessList>(); List<HappinessList> lii3 = new List<HappinessList>(); List<HappinessList> lii4 = new List<HappinessList>(); int minid = con.HappinessList.Min(r => r.Ids); int mo = minid % 4; for (int i = minid; i <= con.HappinessList.Max(r=>r.Ids); i++) { if (i % 4 == 1) { HappinessList li1 = new HappinessList(); li1=con.HappinessList.Where(r => r.Ids == i).FirstOrDefault(); lii1.Add(li1); } if (i % 4 == 2) { HappinessList li2 = new HappinessList(); li2 = con.HappinessList.Where(r => r.Ids == i).FirstOrDefault(); lii2.Add(li2); } if (i % 4 == 3) { HappinessList li3 = new HappinessList(); li3 = con.HappinessList.Where(r => r.Ids == i).FirstOrDefault(); lii3.Add(li3); } if (i % 4 == 0) { HappinessList li4 = new HappinessList(); li4 = con.HappinessList.Where(r => r.Ids == i).FirstOrDefault(); lii4.Add(li4); } } Repeater2.DataSource =lii1 ; Repeater2.DataBind(); Repeater3.DataSource = lii2; Repeater3.DataBind(); Repeater4.DataSource = lii3; Repeater4.DataBind(); Repeater5.DataSource = lii4; Repeater5.DataBind(); }