Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download database SQL from here.
You cannot directly set the Id value of label using Eval. This will give you the error like
The ID property of a control can only be set using the ID attribute in the tag and a simple value.
So i have used HiddenField and OnItemDataBound event for setting the Label Id as Column cell value.
HTML
<asp:Repeater ID="rptUserNames" runat="server" OnItemDataBound="UserNames_OnItemDataBound">
<ItemTemplate>
<asp:HiddenField runat="server" ID="hfName" Value='<%# Eval("Name") %>' />
<asp:Label ID="lblName" Text="<%#DateTime.Now.AddDays(Container.ItemIndex + 1) %>"
runat="server" />
</ItemTemplate>
<SeparatorTemplate>
<br />
</SeparatorTemplate>
</asp:Repeater>
Namespaces
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
this.rptUserNames.DataSource = dt;
this.rptUserNames.DataBind();
}
}
}
}
}
protected void UserNames_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Label lbl = e.Item.FindControl("lblName") as Label;
lbl.ID = (e.Item.FindControl("hfName") as HiddenField).Value;
}
}