repeater实现多列显示


一、下面的代码实现,每行显示三列

前台代码:
<table>
<tr>
<asp:repeater id="Repeater1" runat="server">
<ItemTemplate>
<td><%#DataBinder.Eval(Container.DataItem,"thing")%></td>
</ItemTemplate>
</asp:repeater>
</tr>
</table>

后台代码:
protected int i=1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {

   XmlSerializer ser = new XmlSerializer(typeof(DataSet));
   DataSet ds = new DataSet("myDataSet");
   DataTable t = new DataTable("table1");
   DataColumn c = new DataColumn("thing");
   t.Columns.Add(c);
   ds.Tables.Add(t);
   DataRow r;
   for(int i = 0; i<10;i++)
   {
    r = t.NewRow();
    r[0] =  i;
    t.Rows.Add(r);
   }
   this.Repeater1 .DataSource =ds.Tables [0].DefaultView ;
   this.Repeater1.DataBind ();
  }

private void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
  {
   if(i%3==0&&i>0)
   {
     e.Item.Controls.Add(new LiteralControl("</tr><tr>"));
   }
   i++;
  }

你可能感兴趣的:(实现)