CS文件内容
- C# code
using System;using System.Linq;using System.Web.UI;using System.Web.UI.WebControls;public partial class Consultation_Statistics_test : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataClassesDataContext DC = new DataClassesDataContext(); var result = DC.用户 .Select(m => new { m.ID, m.姓名 }); Repeater1.DataSource = result; Repeater1.DataBind(); } } protected void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e) { Label IDLabel = e.Item.FindControl("IDLabel") as Label; Label XMLabel = e.Item.FindControl("XMLabel") as Label; IDLabel.Text = DataBinder.Eval(e.Item.DataItem, "ID").ToString(); XMLabel.Text = DataBinder.Eval(e.Item.DataItem, "姓名").ToString(); }}
ASPX文件内容
- HTML code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="Consultation_Statistics_test" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div> <table> <tr> <td>ID</td> <td>姓名</td> </tr> <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_OnItemDataBound"> <ItemTemplate> <tr> <td> <asp:Label ID="IDLabel" runat="server" Text="Label"></asp:Label></td> <td> <asp:Label ID="XMlabel" runat="server" Text="Label"></asp:Label></td> </tr> </ItemTemplate> </asp:Repeater> </table> </div> </form></body></html>
------解决方案--------------------------------------------------------
- HTML code
<asp:Label ID="valueLabel" runat="server" Text='<%# Eval("ID") %>' />
------解决方案--------------------------------------------------------
Repeater要么用<%# Eval() %> 要么用OnItemDataBound吧,我想用它的目的就在于灵活性大,<ItemTemplate>里放的什么控件,只有你自己绑定,自己手动来实现!这是这样想的!
如果全自动实现你就用GridView!
另外,我不清楚你非要这样做的目的是什么