repeater一个简单的用法例子
(前台)
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound"
onitemcommand="Repeater1_ItemCommand">
<HeaderTemplate><table width="500px"><tr><td>用户名</td><td>评论内容</td><td>时间</td></tr></HeaderTemplate>
<ItemTemplate><tr runat="server" id="trRow"><td><%#Eval("UserName")%></td><td><%#Eval("Contents")%></td><td><%#Eval("discusstime")%></td>
</tr></ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
(后台)
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
{
string str1 = @"server=Rose-PC\SQLEXPRESS;Database=New;User Id=sa;password= ";
string str2 = "select * from T_discussInfo";
using (SqlConnection conn = new SqlConnection(str1))
{
conn.Open();
using (SqlCommand comm = new SqlCommand(str2, conn))
{ string ID = Request["ID"];
SqlDataAdapter adapter = new SqlDataAdapter(comm);
DataSet Ds = new DataSet();
adapter.Fill(Ds);
Repeater1.DataSource = Ds;
Repeater1.DataBind();
}
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView rowView = (DataRowView)e.Item.DataItem;
}