This article shows how to bind data in data controls without using any database and also shows paging on controls. We will learn here how to bind controls with data without a database and how to handle paging on controls.

Initial Chamber


Step 1

Open your Visual Studio and create an empty website then provide a suitable name such as DataControlExp.

Step 2

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

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

Design Chamber

Step 3

Open the DataControlExp.aspx file and write some code for the design of the application.

This is the ListView design phase.

Here I've designed the ListView Control and used some property as in the following:

  1. <div>
  2. <h3>ListView</h3>
  3. <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
  4. ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
  5. <LayoutTemplate>
  6. <table border="1">
  7. <tr>
  8. <th>Product </th>
  9. <th>Quantity </th>
  10. <th>Price </th>
  11. </tr>
  12. <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
  13. <tr>
  14. <td colspan="3">
  15. <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="2">
  16. <Fields>
  17. <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
  18. ShowNextPageButton="false" />
  19. <asp:NumericPagerField ButtonType="Link" />
  20. <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
  21. </Fields>
  22. </asp:DataPager>
  23. </td>
  24. </tr>
  25. </table>
  26. </LayoutTemplate>
  27. <GroupTemplate>
  28. <tr>
  29. <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
  30. </tr>
  31. </GroupTemplate>
  32. <ItemTemplate>
  33. <td><%# Eval("Product_Name") %>
  34. </td>
  35. <td><%# Eval("Quantity") %>
  36. </td>
  37. <td><%# Eval("Price") %>
  38. </td>
  39. </ItemTemplate>
  40. </asp:ListView>
  41. </div>

FormView control

I've designed a FormView control as in the following:

  1. <h3>FormView</h3>
  2. <div>
  3. <asp:FormView ID="FormView1" runat="server"
  4. AllowPaging="True" EnableViewState="False" OnPageIndexChanging="FormView1_PageIndexChanging" BackColor="Green">
  5. <ItemTemplate>
  6. <hr />
  7. <h3><%# Eval("Product_Name") %>
  8. </h3>
  9. <table border="0">
  10. <tr>
  11. <td class="ProductPropertyLabel">Quantity:</td>
  12. <td class="ProductPropertyValue"><%# Eval("Quantity") %>
  13. </td>
  14. <td class="ProductPropertyLabel">Price:</td>
  15. <td class="ProductPropertyValue"><%# Eval("Price")%>
  16. </td>
  17. </tr>
  18. </table>
  19. <hr />
  20. </ItemTemplate>
  21. </asp:FormView>
  22. </div>
  23. Here design DetailsView-
  24. <h3>DetailsView</h3>
  25. <div>
  26. <div>
  27. <asp:DetailsView ID="DetailsView1" OnPageIndexChanging="DetailsView1_PageIndexChanging" BackColor="Wheat"
  28. AllowPaging="true" runat="server" />
  29. </div>
  30. </div>
  31. Here design GridView-
  32. <h3>GridView</h3>
  33. <div>
  34. <asp:GridView ID="gvExample" runat="server" AllowPaging="true" PageSize="2" AutoGenerateColumns="False" BackColor="PINK" BorderColor="#E7E7FF"
  35. BorderWidth="1px" CellPadding="3" OnPageIndexChanging="gvExample_PageIndexChanging">
  36. <Columns>
  37. <asp:TemplateField HeaderText="Product_Name">
  38. <ItemTemplate>
  39. <asp:Label ID="lblProduct" runat="server" Text='<%#Eval("Product_Name")%>'>
  40. </asp:Label>
  41. </ItemTemplate>
  42. </asp:TemplateField>
  43. <asp:TemplateField HeaderText="Quantity">
  44. <ItemTemplate>
  45. <asp:Label ID="lblQuantity" runat="server" Text='<%#Eval("Quantity")%>'>
  46. </asp:Label>
  47. </ItemTemplate>
  48. </asp:TemplateField>
  49. <asp:TemplateField HeaderText="Price">
  50. <ItemTemplate>
  51. <asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Price")%>'>
  52. </asp:Label>
  53. </ItemTemplate>
  54. </asp:TemplateField>
  55. </Columns>
  56. </asp:GridView>
  57. </div>
The page looks as in the following.

DataControlExp.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataControlExp.aspx.cs" Inherits="DataControlExp" %>
  2. <!DOCTYPE html>
  3. <html
  4. xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6. <title></title>
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. <div style="width: 50%">
  12. <div>
  13. <h3>ListView</h3>
  14. <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
  15. ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
  16. <LayoutTemplate>
  17. <table border="1">
  18. <tr>
  19. <th>Product
  20. </th>
  21. <th>Quantity
  22. </th>
  23. <th>Price
  24. </th>
  25. </tr>
  26. <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
  27. <tr>
  28. <td colspan="3">
  29. <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="2">
  30. <Fields>
  31. <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
  32. ShowNextPageButton="false" />
  33. <asp:NumericPagerField ButtonType="Link" />
  34. <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
  35. </Fields>
  36. </asp:DataPager>
  37. </td>
  38. </tr>
  39. </table>
  40. </LayoutTemplate>
  41. <GroupTemplate>
  42. <tr>
  43. <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
  44. </tr>
  45. </GroupTemplate>
  46. <ItemTemplate>
  47. <td><%# Eval("Product_Name") %>
  48. </td>
  49. <td><%# Eval("Quantity") %>
  50. </td>
  51. <td><%# Eval("Price") %>
  52. </td>
  53. </ItemTemplate>
  54. </asp:ListView>
  55. </div>
  56. <br />
  57. <h3>FormView</h3>
  58. <div>
  59. <asp:FormView ID="FormView1" runat="server"
  60. AllowPaging="True" EnableViewState="False" OnPageIndexChanging="FormView1_PageIndexChanging" BackColor="Green">
  61. <ItemTemplate>
  62. <hr />
  63. <h3><%# Eval("Product_Name") %>
  64. </h3>
  65. <table border="0">
  66. <tr>
  67. <td class="ProductPropertyLabel">Quantity:</td>
  68. <td class="ProductPropertyValue"><%# Eval("Quantity") %>
  69. </td>
  70. <td class="ProductPropertyLabel">Price:</td>
  71. <td class="ProductPropertyValue"><%# Eval("Price")%>
  72. </td>
  73. </tr>
  74. </table>
  75. <hr />
  76. </ItemTemplate>
  77. </asp:FormView>
  78. </div>
  79. </div>
  80. <div style="width: 50%">
  81. <h3>DetailsView</h3>
  82. <div>
  83. <div>
  84. <asp:DetailsView ID="DetailsView1" OnPageIndexChanging="DetailsView1_PageIndexChanging" BackColor="Wheat"
  85. AllowPaging="true" runat="server" />
  86. </div>
  87. </div>
  88. <h3>GridView</h3>
  89. <div>
  90. <asp:GridView ID="gvExample" runat="server" AllowPaging="true" PageSize="2" AutoGenerateColumns="False" BackColor="PINK" BorderColor="#E7E7FF"
  91. BorderWidth="1px" CellPadding="3" OnPageIndexChanging="gvExample_PageIndexChanging">
  92. <Columns>
  93. <asp:TemplateField HeaderText="Product_Name">
  94. <ItemTemplate>
  95. <asp:Label ID="lblProduct" runat="server" Text='<%#Eval("Product_Name")%>'>
  96. </asp:Label>
  97. </ItemTemplate>
  98. </asp:TemplateField>
  99. <asp:TemplateField HeaderText="Quantity">
  100. <ItemTemplate>
  101. <asp:Label ID="lblQuantity" runat="server" Text='<%#Eval("Quantity")%>'>
  102. </asp:Label>
  103. </ItemTemplate>
  104. </asp:TemplateField>
  105. <asp:TemplateField HeaderText="Price">
  106. <ItemTemplate>
  107. <asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Price")%>'>
  108. </asp:Label>
  109. </ItemTemplate>
  110. </asp:TemplateField>
  111. </Columns>
  112. </asp:GridView>
  113. </div>
  114. </div>
  115. </div>
  116. </form>
  117. </body>
  118. </html>
CODE CHAMBER

Step 4

In the code chamber we will write some code so that our application works.

DataControlExp.aspx.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. public partial class DataControlExp: System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (!IsPostBack) {
  14. BindListView();
  15. BindFormView();
  16. BindDetailsView();
  17. BindGridView();
  18. }
  19. }
  20. protected void BindListView()
  21. {
  22. //Creating a dataset
  23. DataSet ds = new DataSet();
  24. DataTable dt;
  25. DataRow dr;
  26. DataColumn pName;
  27. DataColumn pQty;
  28. DataColumn pPrice;
  29. //create an object of datatable
  30. dt = new DataTable();
  31. //creating column of datatable with datatype
  32. pName = new DataColumn("Product_Name", Type.GetType("System.String"));
  33. pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
  34. pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
  35. //bind data table columns in datatable
  36. dt.Columns.Add(pName);
  37. dt.Columns.Add(pQty);
  38. dt.Columns.Add(pPrice);
  39. //creating data row and assiging the value to columns of datatable
  40. dr = dt.NewRow();
  41. dr["Product_Name"] = "Product 1";
  42. dr["Quantity"] = 2;
  43. dr["Price"] = 200;
  44. dt.Rows.Add(dr);
  45. dr = dt.NewRow();
  46. dr["Product_Name"] = "Product 2";
  47. dr["Quantity"] = 5;
  48. dr["Price"] = 480;
  49. dt.Rows.Add(dr);
  50. dr = dt.NewRow();
  51. dr["Product_Name"] = "Product 3";
  52. dr["Quantity"] = 8;
  53. dr["Price"] = 100;
  54. dt.Rows.Add(dr);
  55. dr = dt.NewRow();
  56. dr["Product_Name"] = "Product 4";
  57. dr["Quantity"] = 2;
  58. dr["Price"] = 500;
  59. dt.Rows.Add(dr);
  60. //Add datatable to the dataset
  61. ds.Tables.Add(dt);
  62. //bind data to data controls
  63. lvCustomers.DataSource = ds.Tables[0];
  64. lvCustomers.DataBind();
  65. }
  66. //paging code
  67. protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
  68. {
  69. (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
  70. this.BindListView();
  71. }
  72. protected void BindFormView()
  73. {
  74. //Creating a dataset
  75. DataSet ds = new DataSet();
  76. DataTable dt;
  77. DataRow dr;
  78. DataColumn pName;
  79. DataColumn pQty;
  80. DataColumn pPrice;
  81. //create an object of datatable
  82. dt = new DataTable();
  83. //creating column of datatable with datatype
  84. pName = new DataColumn("Product_Name", Type.GetType("System.String"));
  85. pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
  86. pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
  87. //bind data table columns in datatable
  88. dt.Columns.Add(pName);
  89. dt.Columns.Add(pQty);
  90. dt.Columns.Add(pPrice);
  91. //creating data row and assiging the value to columns of datatable
  92. dr = dt.NewRow();
  93. dr["Product_Name"] = "Product 1";
  94. dr["Quantity"] = 2;
  95. dr["Price"] = 200;
  96. dt.Rows.Add(dr);
  97. dr = dt.NewRow();
  98. dr["Product_Name"] = "Product 2";
  99. dr["Quantity"] = 5;
  100. dr["Price"] = 480;
  101. dt.Rows.Add(dr);
  102. dr = dt.NewRow();
  103. dr["Product_Name"] = "Product 3";
  104. dr["Quantity"] = 8;
  105. dr["Price"] = 100;
  106. dt.Rows.Add(dr);
  107. dr = dt.NewRow();
  108. dr["Product_Name"] = "Product 4";
  109. dr["Quantity"] = 2;
  110. dr["Price"] = 500;
  111. dt.Rows.Add(dr);
  112. //Add datatable to the dataset
  113. ds.Tables.Add(dt);
  114. //bind data to data controls
  115. FormView1.DataSource = ds.Tables[0];
  116. FormView1.DataBind();
  117. }
  118. //paging code
  119. protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
  120. {
  121. FormView1.PageIndex = e.NewPageIndex;
  122. BindFormView();
  123. }
  124. protected void BindDetailsView()
  125. {
  126. //Creating a dataset
  127. DataSet ds = new DataSet();
  128. DataTable dt;
  129. DataRow dr;
  130. DataColumn pName;
  131. DataColumn pQty;
  132. DataColumn pPrice;
  133. //create an object of datatable
  134. dt = new DataTable();
  135. //creating column of datatable with datatype
  136. pName = new DataColumn("Product_Name", Type.GetType("System.String"));
  137. pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
  138. pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
  139. //bind data table columns in datatable
  140. dt.Columns.Add(pName);
  141. dt.Columns.Add(pQty);
  142. dt.Columns.Add(pPrice);
  143. //creating data row and assiging the value to columns of datatable
  144. dr = dt.NewRow();
  145. dr["Product_Name"] = "Product 1";
  146. dr["Quantity"] = 2;
  147. dr["Price"] = 200;
  148. dt.Rows.Add(dr);
  149. dr = dt.NewRow();
  150. dr["Product_Name"] = "Product 2";
  151. dr["Quantity"] = 5;
  152. dr["Price"] = 480;
  153. dt.Rows.Add(dr);
  154. dr = dt.NewRow();
  155. dr["Product_Name"] = "Product 3";
  156. dr["Quantity"] = 8;
  157. dr["Price"] = 100;
  158. dt.Rows.Add(dr);
  159. dr = dt.NewRow();
  160. dr["Product_Name"] = "Product 4";
  161. dr["Quantity"] = 2;
  162. dr["Price"] = 500;
  163. dt.Rows.Add(dr);
  164. //Add datatable to the dataset
  165. ds.Tables.Add(dt);
  166. //bind data to data controls
  167. DetailsView1.DataSource = ds.Tables[0];
  168. DetailsView1.DataBind();
  169. }
  170. //paging code
  171. protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
  172. {
  173. DetailsView1.PageIndex = e.NewPageIndex;
  174. BindDetailsView();
  175. }
  176. protected void BindGridView()
  177. {
  178. //Creating a dataset
  179. DataSet ds = new DataSet();
  180. DataTable dt;
  181. DataRow dr;
  182. DataColumn pName;
  183. DataColumn pQty;
  184. DataColumn pPrice;
  185. //create an object of datatable
  186. dt = new DataTable();
  187. //creating column of datatable with datatype
  188. pName = new DataColumn("Product_Name", Type.GetType("System.String"));
  189. pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
  190. pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
  191. //bind data table columns in datatable
  192. dt.Columns.Add(pName);
  193. dt.Columns.Add(pQty);
  194. dt.Columns.Add(pPrice);
  195. //creating data row and assiging the value to columns of datatable
  196. dr = dt.NewRow();
  197. dr["Product_Name"] = "Product 1";
  198. dr["Quantity"] = 2;
  199. dr["Price"] = 200;
  200. dt.Rows.Add(dr);
  201. dr = dt.NewRow();
  202. dr["Product_Name"] = "Product 2";
  203. dr["Quantity"] = 5;
  204. dr["Price"] = 480;
  205. dt.Rows.Add(dr);
  206. dr = dt.NewRow();
  207. dr["Product_Name"] = "Product 3";
  208. dr["Quantity"] = 8;
  209. dr["Price"] = 100;
  210. dt.Rows.Add(dr);
  211. dr = dt.NewRow();
  212. dr["Product_Name"] = "Product 4";
  213. dr["Quantity"] = 2;
  214. dr["Price"] = 500;
  215. dt.Rows.Add(dr);
  216. //Add datatable to the dataset
  217. ds.Tables.Add(dt);
  218. //bind data to data controls
  219. gvExample.DataSource = ds.Tables[0];
  220. gvExample.DataBind();
  221. }
  222. //paging code
  223. protected void gvExample_PageIndexChanging(object sender, GridViewPageEventArgs e)
  224. {
  225. gvExample.PageIndex = e.NewPageIndex;
  226. BindGridView();
  227. }
  228. }

Output

Figure 1:Output

I hope you liked this. Have a good day. Thank you for reading.