網頁端程序小知識

1、LinQ Distinct某字段去重javascript

  新建類GoodsIdComparer,繼承 IEqualityComparer<Goods>,實現Equals方法java

    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());//須要引用命名空間數據庫

2、MVC的控制器Controllers中用using直接調用數據庫組合查詢app

  在MVC的控制器Controllers中用using直接調用數據庫組合查詢,返回視圖時應注意爲:ide

  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));    

 

  視圖中引用強類型:ui

  @model List<Goods>this

3、LinQ查詢數據庫中自增列ID的最大值spa

 

        public int Maxid()
        {
            return con.Goods.Max(r=>r.Gids);
        }

 4、MARQUEE無縫滾動code

  

  <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>

 5、返回頂部小按鈕

  在網頁開頭寫一個帶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>

 6、瀑布流佈局

  前臺界面上:

    首先一個大的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();
                }
後臺綁定
相關文章
相關標籤/搜索