繁体   English   中英

在 ASPX 网页中显示对象数据列表

[英]Display list of objects data in a ASPX web page

我正在尝试在网页中显示一个简单的对象数据列表。 我只让标签显示在输出中,没有数据。 请告诉我这段代码中可能有什么错误。

我得到以下输出

姓名
年龄
城市

姓名
年龄
城市

姓名
年龄
城市

期望的输出是

名字唐娜
40岁
纽约市

名字拉吉
10岁
纽约市

名称艺术
16岁
纽约市

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


     public partial class Customer : System.Web.UI.Page
     {

         public class myCustomer {
             public String Name {get;set;}
             public int Age { get; set; }
             public String City { get; set; }

             public myCustomer()
             {
             }

             public myCustomer(string _name, int _age, string _city)
             {
                 Name = _name;
                 Age = _age;
                 City = _city;
             }

     }

         List<myCustomer> customerList;

         protected void Page_Load(object sender, EventArgs e)
         {
             customerList = new List<myCustomer>();

             myCustomer co1 = new myCustomer { Name = "Donna", Age = 40, City = "New York" };
             myCustomer co2 = new myCustomer("Raj", 10, "New York");
             myCustomer co3 = new myCustomer("Art", 16, "New York");
             customerList.Add(co1);
             customerList.Add(co2);
             customerList.Add(co3);
             testDataGrid.DataSource = customerList;
             testDataGrid.DataBind();
         }        
 }


<%@ Page Language="C#" CodeFile="customer.aspx.cs" Inherits="Customer" %>

<!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="testDataGrid" runat="server" >

        <ItemTemplate>
            <table>
            <tr><td>Name</td><td><asp:TextBox  ID="Customer Name" Text= '<%# Eval("Name") %>' visible="true"/> </td></tr>
            <tr><td>Age</td><td><asp:TextBox ID="Age" Text= '<%# Eval("Age") %>'visible="true" /></td></tr>
            <tr><td>City</td><td><asp:TextBox  ID="City" Text='<%# Eval("City") %>' visible="true" /></td></tr>
                </table>
          </ItemTemplate>

    </asp:Repeater>
    </div>
    </form>
</body>
</html>

你的文本框需要一个 runat="server"

 <asp:TextBox id="tbName" runat="server" Text='<%#Eval("Name") %>'/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM