We will learn here how to bind controls with data without a database and how to handle paging on controls. This binding of data depends on the link button text on link button click event.

Also read:

Initial Chamber

Step 1

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

Step 2

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

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

Design Chamber

Step 3

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

Step 3.1

Add the following style sheet code to the head chamber of the page:

  1. <style type="text/css">
  2. .linkCat {
  3. background-color:aqua;
  4. }
  5. </style>

Set your style of page depending on your needs.

Step 3.2

Choose the control from the toolbox and make your design page like:

  1. <div>
  2. <h3>ListView</h3>
  3. <%-- Linkbutton --%>
  4. <asp:LinkButton ID="lnkCat1" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Cat1" OnClick="ENameLinkBtn_Click" CommandArgument="Cat1"></asp:LinkButton>
  5. <asp:LinkButton ID="lnkCat2" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Cat2" OnClick="ENameLinkBtn_Click" CommandArgument="Cat2"></asp:LinkButton>
  6. <asp:LinkButton ID="lnkCat3" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Cat3" OnClick="ENameLinkBtn_Click" CommandArgument="Cat3"></asp:LinkButton>
  7. <asp:HiddenField ID="hdnText" runat="server" ClientIDMode="Static" Value="" />
  8. <%-- end --%>
  9. <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
  10. ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
  11. <LayoutTemplate>
  12. <table border="1">
  13. <tr>
  14. <th>Product
  15. </th>
  16. <th>Quantity
  17. </th>
  18. <th>Price
  19. </th>
  20. <th>Category
  21. </th>
  22. </tr>
  23. <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
  24. <tr>
  25. <td colspan="3">
  26. <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="2">
  27. <Fields>
  28. <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
  29. ShowNextPageButton="false" />
  30. <asp:NumericPagerField ButtonType="Link" />
  31. <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
  32. </Fields>
  33. </asp:DataPager>
  34. </td>
  35. </tr>
  36. </table>
  37. </LayoutTemplate>
  38. <GroupTemplate>
  39. <tr>
  40. <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
  41. </tr>
  42. </GroupTemplate>
  43. <ItemTemplate>
  44. <td>
  45. <%# Eval("Product_Name") %>
  46. </td>
  47. <td>
  48. <%# Eval("Quantity") %>
  49. </td>
  50. <td>
  51. <%# Eval("Price") %>
  52. </td>
  53. <td>
  54. <%# Eval("Category") %>
  55. </td>
  56. </ItemTemplate>
  57. </asp:ListView>
  58. </div>

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

Design Page

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListViewLinkBtn.aspx.cs" Inherits="ListViewLinkBtn" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <style type="text/css">
  7. .linkCat {
  8. background-color:aqua;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <form id="form1" runat="server">
  14. <div>
  15. <h3>ListView</h3>
  16. <%-- Linkbutton --%>
  17. <asp:LinkButton ID="lnkCat1" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Cat1" OnClick="ENameLinkBtn_Click" CommandArgument="Cat1"></asp:LinkButton>
  18. <asp:LinkButton ID="lnkCat2" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Cat2" OnClick="ENameLinkBtn_Click" CommandArgument="Cat2"></asp:LinkButton>
  19. <asp:LinkButton ID="lnkCat3" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Cat3" OnClick="ENameLinkBtn_Click" CommandArgument="Cat3"></asp:LinkButton>
  20. <asp:HiddenField ID="hdnText" runat="server" ClientIDMode="Static" Value="" />
  21. <%-- end --%>
  22. <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
  23. ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
  24. <LayoutTemplate>
  25. <table border="1">
  26. <tr>
  27. <th>Product
  28. </th>
  29. <th>Quantity
  30. </th>
  31. <th>Price
  32. </th>
  33. <th>Category
  34. </th>
  35. </tr>
  36. <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
  37. <tr>
  38. <td colspan="3">
  39. <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="2">
  40. <Fields>
  41. <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
  42. ShowNextPageButton="false" />
  43. <asp:NumericPagerField ButtonType="Link" />
  44. <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
  45. </Fields>
  46. </asp:DataPager>
  47. </td>
  48. </tr>
  49. </table>
  50. </LayoutTemplate>
  51. <GroupTemplate>
  52. <tr>
  53. <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
  54. </tr>
  55. </GroupTemplate>
  56. <ItemTemplate>
  57. <td>
  58. <%# Eval("Product_Name") %>
  59. </td>
  60. <td>
  61. <%# Eval("Quantity") %>
  62. </td>
  63. <td>
  64. <%# Eval("Price") %>
  65. </td>
  66. <td>
  67. <%# Eval("Category") %>
  68. </td>
  69. </ItemTemplate>
  70. </asp:ListView>
  71. </div>
  72. </form>
  73. </body>
  74. </html>

Your design looks as in Figure 1.

design
Figure 1

Code Chamber

Step 4

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

Add the following namespaces to the namespace section of your code behind page.

  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;

Now your page looks as in the following.

Code behind Page

  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 ListViewLinkBtn : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (!IsPostBack) {
  14. BindListView("");
  15. }
  16. }
  17. protected void BindListView(string sGroup)
  18. {
  19. //Creating a dataset
  20. DataSet ds = new DataSet();
  21. DataTable dt;
  22. DataRow dr;
  23. DataColumn pName;
  24. DataColumn pQty;
  25. DataColumn pPrice;
  26. DataColumn pCategory;
  27. //create an object of datatable
  28. dt = new DataTable();
  29. //creating column of datatable with datatype
  30. pName = new DataColumn("Product_Name", Type.GetType("System.String"));
  31. pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
  32. pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
  33. pCategory = new DataColumn("Category", Type.GetType("System.String"));
  34. //bind data table columns in datatable
  35. dt.Columns.Add(pName);
  36. dt.Columns.Add(pQty);
  37. dt.Columns.Add(pPrice);
  38. dt.Columns.Add(pCategory);
  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. dr["Category"] = "Cat1";
  45. dt.Rows.Add(dr);
  46. dr = dt.NewRow();
  47. dr["Product_Name"] = "Product 2";
  48. dr["Quantity"] = 5;
  49. dr["Price"] = 480;
  50. dr["Category"] = "Cat2";
  51. dt.Rows.Add(dr);
  52. dr = dt.NewRow();
  53. dr["Product_Name"] = "Product 3";
  54. dr["Quantity"] = 8;
  55. dr["Price"] = 100;
  56. dr["Category"] = "Cat1";
  57. dt.Rows.Add(dr);
  58. dr = dt.NewRow();
  59. dr["Product_Name"] = "Product 4";
  60. dr["Quantity"] = 2;
  61. dr["Price"] = 500;
  62. dr["Category"] = "Cat2";
  63. dt.Rows.Add(dr);
  64. dr = dt.NewRow();
  65. dr["Product_Name"] = "Product 5";
  66. dr["Quantity"] = 8;
  67. dr["Price"] = 1050;
  68. dr["Category"] = "Cat3";
  69. dt.Rows.Add(dr);
  70. dr = dt.NewRow();
  71. dr["Product_Name"] = "Product 6";
  72. dr["Quantity"] = 2;
  73. dr["Price"] = 5005;
  74. dr["Category"] = "Cat3";
  75. dt.Rows.Add(dr);
  76. dr = dt.NewRow();
  77. dr["Product_Name"] = "Product 7";
  78. dr["Quantity"] = 8;
  79. dr["Price"] = 1000;
  80. dr["Category"] = "Cat3";
  81. dt.Rows.Add(dr);
  82. dr = dt.NewRow();
  83. dr["Product_Name"] = "Product 8";
  84. dr["Quantity"] = 2;
  85. dr["Price"] = 5000;
  86. dr["Category"] = "Cat3";
  87. dt.Rows.Add(dr);
  88. //Add datatable to the dataset
  89. ds.Tables.Add(dt);
  90. if (sGroup != "")
  91. {
  92. var dv = ds.Tables[0].DefaultView;
  93. dv.RowFilter = "Category='" + sGroup + "'";
  94. DataSet ds1 = new DataSet();
  95. var newdt = dv.ToTable();
  96. ds1.Tables.Add(newdt);
  97. //bind data to data controls
  98. lvCustomers.DataSource = ds1.Tables[0];
  99. lvCustomers.DataBind();
  100. }
  101. else {
  102. lvCustomers.DataSource = ds.Tables[0];
  103. lvCustomers.DataBind();
  104. }
  105. }
  106. //paging code
  107. protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
  108. {
  109. (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
  110. if (hdnText.Value != "")
  111. {
  112. string yourValue = hdnText.Value.ToString();
  113. this.BindListView(yourValue);
  114. }
  115. else
  116. {
  117. this.BindListView("");
  118. }
  119. }
  120. protected void ENameLinkBtn_Click(object sender, EventArgs e)
  121. {
  122. LinkButton btn = (LinkButton)(sender);
  123. string yourValue = btn.CommandArgument;
  124. // do what you need here
  125. hdnText.Value = yourValue;
  126. BindListView(yourValue);
  127. }
  128. }

Output

On initial load:

On initial load
Figure 2

On first link button click:

On first link button click
Figure 3

On second link button click:

On 2nd link button click
Figure 4

On third link button click:

On 3rd link button click
Figure 5

On third link page click:

list view
Figure 6

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