将GridView绑定到<li>

时间:2016-06-01 04:51:18

标签: c# asp.net

是否可以将gridview与li绑定,以便填充的数据都具有li标签?

<asp:GridView ID="GridView_foodList" Width="100%" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource_GetFoodNotEntered"
        OnDataBound="GridView_foodList_DataBound" CellPadding="4" ForeColor="#222222"
        BorderColor="#FFA8B0" BorderWidth="2px" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="NAME" HeaderText="Name">
                <ItemStyle BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" Width="70px" />
                <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
          <asp:BoundField DataField="PRICE" HeaderText="Price">
                <ItemStyle BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" Width="70px" />
                <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
</Columns></asp:GridView>

1 个答案:

答案 0 :(得分:2)

ASP.NET GridView控件生成一个表。如果要生成更有自定义的内容,如有序或无序列表,则应使用Repeater控件。

代码背后:

public partial class aaa_useRepeater : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var list = GetData();

        Repeater1.DataSource = list;
        Repeater1.DataBind();
    }

    private List<Prod> GetData()
    {

        var p1 = new Prod { ID = 0, Name = "Product 1" };
        var p2 = new Prod { ID = 1, Name = "Product 2" };
        var p3 = new Prod { ID = 2, Name = "Product 3" };
        var p4 = new Prod { ID = 3, Name = "Product 4" };

        var list = new List<Prod> { p1, p2, p3, p4 };
        return list;
    }
}

public class Prod
{
    public int ID { get; set; }
    public string Name { get; set; }
}

<强> .ASPX:

<ul>
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <li><%# Eval("Name") %></li>
        </ItemTemplate>
    </asp:Repeater>
</ul>

<强>输出:

<ul>
    <li>Product 1</li>
    <li>Product 2</li>
    <li>Product 3</li>
    <li>Product 4</li>
</ul>