The Repeater control is used to display a repeated list of items
The Repeater is a Data control which is used to show / bind dynamic data
Repeater has 5 inline template to format it :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP Literal</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="text-align: center">ASP Repeater Example</h2>
<asp:Repeater runat="server" ID="rgallery">
<HeaderTemplate>
<h3 style="text-align:center">Current Photos</h3>
</HeaderTemplate>
<ItemTemplate>
<div class="col-md-4" style="">
<asp:Image runat="server" Style="width:50%" ImageUrl='<%# Eval("Path") %>' />
<asp:Label runat="server" ID="photoid" style="display:none" Text='<%# Eval("Srno") %>'></asp:Label>
<asp:LinkButton runat="server" ID="lbtndelete" OnClick="lbtndelete_Click" OnClientClick="return confirm('Do you want to delete this Photo')" Style="background-color:orange;color:white;padding:15px"><span class="fa fa-trash"></span> Delete</asp:LinkButton>
</div>
</ItemTemplate>
<FooterTemplate>
<h3 style="text-align:center">Thank you...!</h3>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Training
{
public partial class Repeaterexample : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
showgalleryphotos();
}
protected void showgalleryphotos()
{
try
{
string constr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Select Srno,Path from Photos", conn))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
rgallery.DataSource = dt;
rgallery.DataBind();
}
}
}
}
catch (Exception es)
{
}
}
protected void lbtndelete_Click(object sender, EventArgs e)
{
try
{
RepeaterItem a = (sender as LinkButton).Parent as RepeaterItem;
int b = int.Parse((a.FindControl("photoid") as Label).Text.ToString());
con.Close();
string qrys = "Select Path from Photos where Srno=" + b;
con.Open();
SqlCommand cmd1 = new SqlCommand(qrys, con);
SqlDataReader sdr = cmd1.ExecuteReader();
if (sdr.HasRows)
{
sdr.Read();
string thumbpath = sdr.GetValue(0).ToString();
System.IO.File.Delete(Server.MapPath(thumbpath));
}
con.Close();
string qry = "delete from Photos where Srno=" + b;
SqlCommand cmd = new SqlCommand(qry, con);
con.Open();
cmd.ExecuteNonQuery();
string message = "Photo Deleted Successfully!";
string script = "window.onload = function(){ alert('";
script += message;
script += "');";
script += "window.location = '";
script += Request.Url.AbsoluteUri;
script += "'; }";
ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
}
catch (Exception es)
{
}
finally
{
con.Close();
}
}
}
}