This article shows you how to bind data in Repeater controls without using any database in ASP.NET. Also, we will learn here how to bind controls with data without using a database. Additionally, how to handle repeater common events will be discussed. For this I have shownthe clicked-on image and changed image on click.

Initial Chamber

Step 1: Open Visual Studio and create an empty website, then provide a suitable name such as RepeaterChangeImage.

Step 2: In Solution Explorer you will get your empty website, then add some web forms.

RepeaterChangeImage (your empty website). Right-click and select Add New Item Web Form. Name it RepeaterChangeImage.aspx.

Design Chamber

Step 3:

Open the RepeaterChangeImage.aspx file and write some code for the design of the application. Choose control from toolbox and put on your design page as in the following code snippet:

  1. <div>
  2. <asp:RepeaterID="Repeater1"runat="server">
  3. <ItemTemplate>
  4. <asp:LabelID="Label1"runat="server"><%#Eval("Product_Name")%></asp:Label>
  5. <asp:LabelID="Label2"runat="server"><%#Eval("Quantity")%></asp:Label>
  6. <asp:LabelID="Label3"runat="server"><%#Eval("Price")%></asp:Label>
  7. <asp:ImageButtonID="ImageButton1"runat="server"Height="50px"Width="100px"ImageUrl='<%#Eval("path") %>'
  8. CommandArgument='<%#Eval("path")%>'OnClick="ImageButton1_Click"/><br/>
  9. </ItemTemplate>
  10. </asp:Repeater>
  11. </div>
Design of page looks like the following:

page

Here, I've designed the GridView Control and uploader used some property as in the following code snippet:
  1. <%@PageLanguage="C#"AutoEventWireup="true"EnableEventValidation="true"CodeFile="ChangeImageRepeater.aspx.cs"Inherits="ChangeImageRepeater"%>
  2. <!DOCTYPEhtml>
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <headrunat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <formid="form1"runat="server">
  9. <div>
  10. <asp:RepeaterID="Repeater1"runat="server">
  11. <ItemTemplate>
  12. <asp:LabelID="Label1"runat="server"><%#Eval("Product_Name")%></asp:Label>
  13. <asp:LabelID="Label2"runat="server"><%#Eval("Quantity")%></asp:Label>
  14. <asp:LabelID="Label3"runat="server"><%#Eval("Price")%></asp:Label>
  15. <asp:ImageButtonID="ImageButton1"runat="server"Height="50px"Width="100px"ImageUrl='<%#Eval("path") %>'
  16. CommandArgument='<%#Eval("path")%>'OnClick="ImageButton1_Click"/><br/>
  17. </ItemTemplate>
  18. </asp:Repeater>
  19. </div>
  20. </form>
  21. </body>
  22. </html>
CODE CHAMBER

Step 4:

In the code chamber we will write some code so that our application works. Adding the following namespaces under namespace section of your code behind page.
  1. using System;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Web;
  5. usingSystem.Web.UI;
  6. usingSystem.Web.UI.WebControls;
  7. usingSystem.Data;
  8. usingSystem.Data.SqlClient;
  9. usingSystem.Configuration;
Code behind Page
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Configuration;
  10. public partial classChangeImageRepeater: System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. if (!IsPostBack)
  15. {
  16. BindRepeater();
  17. }
  18. }
  19. protected void BindRepeater()
  20. {
  21. DataSet ds = newDataSet();
  22. DataTabledt;
  23. DataRowdr;
  24. DataColumnpName;
  25. DataColumnpQty;
  26. DataColumnpPrice;
  27. DataColumnpPath;
  28. //int i = 0;
  29. dt = newDataTable();
  30. pName = newDataColumn("Product_Name", Type.GetType("System.String"));
  31. pQty = newDataColumn("Quantity", Type.GetType("System.Int32"));
  32. pPrice = newDataColumn("Price", Type.GetType("System.Int32"));
  33. pPath = newDataColumn("path", Type.GetType("System.String"));
  34. dt.Columns.Add(pName);
  35. dt.Columns.Add(pQty);
  36. dt.Columns.Add(pPrice);
  37. dt.Columns.Add(pPath);
  38. dr = dt.NewRow();
  39. dr["Product_Name"] = "Product 1";
  40. dr["Quantity"] = 2;
  41. dr["Price"] = 200;
  42. dr["path"] = "image/Produc1.jpeg";
  43. dt.Rows.Add(dr);
  44. dr = dt.NewRow();
  45. dr["Product_Name"] = "Product 2";
  46. dr["Quantity"] = 5;
  47. dr["Price"] = 480;
  48. dr["path"] = "image/Produc2.jpeg";
  49. dt.Rows.Add(dr);
  50. dr = dt.NewRow();
  51. dr["Product_Name"] = "Product 3";
  52. dr["Quantity"] = 8;
  53. dr["Price"] = 100;
  54. dr["path"] = "image/Produc3.jpeg";
  55. dt.Rows.Add(dr);
  56. dr = dt.NewRow();
  57. dr["Product_Name"] = "Product 4";
  58. dr["Quantity"] = 2;
  59. dr["Price"] = 500;
  60. dr["path"] = "image/Produc4.jpeg";
  61. dt.Rows.Add(dr);
  62. ds.Tables.Add(dt);
  63. Repeater1.DataSource = ds.Tables[0];
  64. Repeater1.DataBind();
  65. }
  66. protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
  67. {
  68. ImageButtonImgBtn = (ImageButton) sender;
  69. string path = ((ImageButton) sender).CommandArgument;
  70. if (ImgBtn.ImageUrl == path)
  71. {
  72. ImgBtn.ImageUrl = "image/Penguins.jpg";
  73. ImgBtn.BackColor = System.Drawing.Color.Aqua;
  74. } else
  75. {
  76. ImgBtn.ImageUrl = path;
  77. ImgBtn.BackColor = System.Drawing.Color.Red;
  78. }
  79. }
  80. }
Output

First Loading:

first

After click on Image:

output

Here you can see how the last image changed on being clicked. I hope you liked this. Have a good day. Thank you for reading.
Read more articles on ASP.NET: