WebForm控件Repeater

我们会发现用拼接字符串来显示一个查询非常的麻烦,有一个控件Repeater帮助你,省去写Foreach

LinQ to SQL类

函数类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// fruitDA 的摘要说明
/// </summary>
public class fruitDA
{
    private DataClasses2DataContext context;
    public fruitDA()
    {
        context = new DataClasses2DataContext();
    }
    public List<fruit> select()
    {
        return context.fruit.ToList();
    }
    public string FriutName(int a)
    {
        var query = context.fruit.Where(p=>p.ids==a);
        if (query.Count() > 0)
        {
            return query.First().name;
        }
        return null;

    }
    public decimal FruitPrice(int a)
    { 
    var query = context.fruit.Where(p=>p.ids==a);
    if (query.Count() > 0)
    {
        return Convert.ToDecimal( query.First().price);

    }
    return 0;
    }

}

新建一页面,BODY中DIV加入Literal标签 后台写函数

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
       <asp:Literal ID="Literal1" runat="server"></asp:Literal>
     <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>               
                <table><tr><td style="color:greenyellow">果果</td>
                    <td style="color:greenyellow">价格</td>
                    <td style="color:greenyellow">哪里滴</td>
                    <td style="color:greenyellow">库存</td></tr>
                    </HeaderTemplate>
                <ItemTemplate>
                <tr bgcolor="Yellow"><td><%#Eval("name") %></td>
                    <td><%#Eval("price") %></td>
                    <td><%#Eval("source") %></td>
                    <td><%#Eval("numbers") %></td>
                    <td><a href='Default3.aspx?ids=<%#Eval("ids") %>'>购买</a></td></tr> 
                </ItemTemplate>
            <AlternatingItemTemplate>
                <tr bgcolor="#f3e"><td><%#Eval("name") %></td>
                    <td><%#Eval("price") %></td><td><%#Eval("source") %></td>
                    <td><%#Eval("numbers") %></td>
                    <td><a href='Default3.aspx?ids=<%#Eval("ids") %>'>购买</a> </td></tr>
            </AlternatingItemTemplate>
            <FooterTemplate> </table></FooterTemplate>
         
        </asp:Repeater>       
   
    </div>
    </form>
</body>
</html>

 

Repeater里面有5个标签,4个常用
<HeaderTemplate>:添加表头.
 <ItemTemplate>:类似于Body要查的数据引用C#代码 <%#Eval("列名")%> 用来传值.
 <AlternatingItemTemplate>: 与<ItemTemplate>替换,先显示<ItemTemplate>.
<FooterTemplate>:表的脚.
后台绑定代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Literal1.Text =f;
        Repeater1.DataSource = new fruitDA().select();
        Repeater1.DataBind();
}
}

这里绑定一个List<>泛型集合,最后要执行绑定 Repeater1.DataBind();

 

posted @ 2015-07-24 00:16  SaYes  阅读(312)  评论(0编辑  收藏  举报