简单的eval表不显示在表中

时间:2013-11-19 20:24:09

标签: c# asp.net

ASP.NET 4.5,

我正在尝试从W.Sanders书中实现一个简单的eval函数。我可以在网站中看到数据,但显示的数据不在表格中。

谢谢。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>



        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <tr>
                    <td> <%# Eval("FirstName")%></td>
                    <td> <%# Eval("LastName")%></td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>



    </div>
    </form>
</body>
</html>

这是.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        SqlConnection cnn = new SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
            cnn.Open();
            SqlCommand cmd = new SqlCommand("SELECT FirstName,LastName FROM Employees", cnn);
            SqlDataReader dr = cmd.ExecuteReader();
            Repeater1.DataSource = dr;
            Repeater1.DataBind();
            cnn.Close();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:5)

您想要表格中的数据,但HTML中没有表格:

<table>
 <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
       <tr>
          <td> <%# Eval("FirstName")%></td>
          <td> <%# Eval("LastName")%></td>
       </tr>
    </ItemTemplate>
 </asp:Repeater>
</table>