using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm9 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Repeater1.DataSource = CreateDataSource();
Repeater1.DataBind();
}
}
protected List<SampleData> CreateDataSource()
{
var list = new List<SampleData>();
for (int i = 0; i < 5; i++)
{
var data = new SampleData
{
Id = i,
Name = "Name-" + i,
Price = 100m * (i + 1),
CheckBoxId = "CheckBox" + i
};
list.Add(data);
}
return list;
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
foreach (Control ctrl in e.Item.Controls)
{
if (ctrl is CheckBox)
{
((CheckBox)ctrl).ID = ((SampleData)e.Item.DataItem).CheckBoxId;
break;
}
}
}
}
}
public class SampleData
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string CheckBoxId { get; set; }
}
}