How To Use Repeater Control in ASP.NET?
Here is the table creating code:
CREATE TABLE tblCustomer
(
[id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Email] [nvarchar](200) NULL,
[Phone] [nvarchar](50) NULL
)
Here is the HTML code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="inserCustomer.aspx.cs" Inherits="inserCustomer" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="table-bordered">
<h3> View Customer by Repeater </h3>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
<th></th>
</tr>
</thead>
<tbody>
<asp:Repeater runat="server" ID="Rp1">
<ItemTemplate>
<tr>
<td> <%#Eval("FirstName") %> </td>
<td> <%#Eval("LastName") %> </td>
<td> <%#Eval("Email") %> </td>
<td> <%#Eval("Phone") %> </td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</div>
</form>
</body>
</html>
Here is the code behind:
ConnectionString:
private readonly SqlConnection con =
new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=AjaxTestProject;Integrated Security=True;");
page load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetCustomer(Rp1);
}
}
Method:
private void GetCustomer(Repeater dl)
{
var dt = new DataTable();
var da = new SqlDataAdapter("SELECT * from tblCustomer", con);
con.Open();
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
dl.DataSource = dt;
dl.DataBind();
}
}
Finally the output look like below:
No comments