Monday, May 12, 2008

binding DataTable to asp:Repeater

html code :

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table width="100%" border="0" cellspacing="0">
<tr><td colspan="2"><b>Personal Database</b></td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="background-color:inherit;" colspan="2"> <%# DataBinder.Eval(Container, "DataItem.Name") %> </td>
</tr>
<tr>
<td> <%# DataBinder.Eval(Container, "DataItem.Age") %>
</td>
<td> <%# DataBinder.Eval(Container, "DataItem.Designation") %>
</td>
</tr>
<tr>
<td>
<a href="<%# DataBinder.Eval(Container, "DataItem.Email") %>">
<%# DataBinder.Eval(Container, "DataItem.Email") %>
</a>
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td height="2" colspan="2"><hr /></td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>


code-behind :

public partial class DetailsPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable("Personal Database");
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(Int32));
dt.Columns.Add("Designation", typeof(string));
dt.Columns.Add("Email", typeof(string));
dt.Rows.Add(new string[4] { "Jack", 24, ".net Programmer", "jackdsouza0@gmail.com" });
dt.Rows.Add(new string[4] { "Jill", 23, ".net Programmer", "jilldsouza0@gmail.com" });
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}

1 comment:

Tone said...

works perfect! Thanks!