We will learn here how to bind controls with data with a database and how to handle paging on controls. The binding will be done depending on the link button text on the link button click event.

Also read

Initial Chamber

Step 1

Open your Visual Studio and create an empty website, then name it ListViewLinkWithDB.

Step 2

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

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

For SQL Server database

Create a database in your SQL Server and name it ProductDB. (You can give your own name instead.)

Database Chamber

Step 3

In ProductDB create a table named Product.

Here's the query:

  1. USE [ProductDb]
  2. GO
  3. /****** Object: Table [dbo].[Product] Script Date: 31.07.2015 10:39:45 AM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. SET ANSI_PADDING ON
  9. GO
  10. CREATE TABLE [dbo].[Product](
  11. [ProductId] [bigint] IDENTITY(1,1) NOT NULL,
  12. [CategoryId] [nchar](10) NULL,
  13. [ProductName] [varchar](50) NULL,
  14. [Qty] [int] NULL,
  15. [Price] [decimal](18, 2) NULL,
  16. [Description] [text] NULL,
  17. CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
  18. (
  19. [ProductId] ASC
  20. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  21. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  22. GO
  23. SET ANSI_PADDING OFF
  24. GO
  25. ALTER TABLE [dbo].[Product] WITH CHECK ADD CONSTRAINT [FK_Product_Product] FOREIGN KEY([ProductId])
  26. REFERENCES [dbo].[Product] ([ProductId])
  27. GO
  28. ALTER TABLE [dbo].[Product] CHECK CONSTRAINT [FK_Product_Product]
  29. GO

The Table Product structure is here:

design view
Figure 1: Tbl_Data

Query execution:

tbl
Figure 2: tbl

Design Chamber

Step 4

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

Step 4.1

Put stylesheet code in the head of the page like the following:

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

Set your style of the page depending on your design needs.

Step 4.2

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

  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="lnkDefault" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Default" OnClick="ENameLinkBtn_Click" CommandArgument="Cat2"></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("ProductName") %>
  46. </td>
  47. <td>
  48. <%# Eval("Qty") %>
  49. </td>
  50. <td>
  51. <%# Eval("Price") %>
  52. </td>
  53. <td>
  54. <%# Eval("CategoryId") %>
  55. </td>
  56. </ItemTemplate>
  57. </asp:ListView>
  58. </div>
Here I've designed the ListView Control and used some property as in the following.

Now the design page looks as in the following.

Design Page
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListVieLinkWithDB.aspx.cs" Inherits="ListVieLinkWithDB" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>ListView Using DataBase</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="lnkDefault" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Default" OnClick="ENameLinkBtn_Click" CommandArgument="Cat2"></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("ProductName") %>
  59. </td>
  60. <td>
  61. <%# Eval("Qty") %>
  62. </td>
  63. <td>
  64. <%# Eval("Price") %>
  65. </td>
  66. <td>
  67. <%# Eval("CategoryId") %>
  68. </td>
  69. </ItemTemplate>
  70. </asp:ListView>
  71. </div>
  72. </form>
  73. </body>
  74. </html>
Your design looks as in Figure 3.

design
Figure 3: Design

Config Chamber

Add the following code to the Web.config.
  1. <connectionStrings>
  2. <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=true;Initial Catalog=ProductDb;" providerName="System.Data.SqlClient"/>
  3. </connectionStrings>
Code Chamber

Step 5

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

Adding the following namespaces to the namespace section of your 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;
Now your page will look the following.

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 class ListVieLinkWithDB : System.Web.UI.Page
  11. {
  12. string connStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15. if (!IsPostBack) {
  16. ListViewControlBind("");
  17. }
  18. }
  19. protected void ListViewControlBind(string sGroup)
  20. {
  21. if (sGroup == "Cat1")
  22. sGroup = "1";
  23. if (sGroup == "Cat2")
  24. sGroup = "2";
  25. else
  26. sGroup = "";
  27. SqlConnection con = new SqlConnection(connStr);
  28. con.Open();
  29. string sQry = @"select ProductName,Qty,Price,CategoryId from dbo.Product";
  30. SqlCommand cmd = new SqlCommand(sQry, con);
  31. SqlDataAdapter da = new SqlDataAdapter(cmd);
  32. DataTable dt = new DataTable();
  33. DataSet ds = new DataSet();
  34. da.Fill(dt);
  35. con.Close();
  36. ds.Tables.Add(dt);
  37. if (sGroup != "")
  38. {
  39. var dv = ds.Tables[0].DefaultView;
  40. dv.RowFilter = "CategoryId='" + sGroup + "'";
  41. DataSet ds1 = new DataSet();
  42. var newdt = dv.ToTable();
  43. ds1.Tables.Add(newdt);
  44. //bind data to data controls
  45. lvCustomers.DataSource = ds1.Tables[0];
  46. lvCustomers.DataBind();
  47. }
  48. else
  49. {
  50. lvCustomers.DataSource = ds.Tables[0];
  51. lvCustomers.DataBind();
  52. }
  53. }
  54. //paging code
  55. protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
  56. {
  57. (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
  58. if (hdnText.Value != "")
  59. {
  60. string yourValue = hdnText.Value.ToString();
  61. if (yourValue == "Default")
  62. {
  63. ListViewControlBind("");
  64. }
  65. else
  66. {
  67. this.ListViewControlBind(yourValue);
  68. }
  69. }
  70. else
  71. {
  72. this.ListViewControlBind("");
  73. }
  74. }
  75. protected void ENameLinkBtn_Click(object sender, EventArgs e)
  76. {
  77. LinkButton btn = (LinkButton)(sender);
  78. string yourValue = btn.CommandArgument;
  79. // do what you need here
  80. if (yourValue == "Default")
  81. {
  82. hdnText.Value = yourValue;
  83. ListViewControlBind("");
  84. }
  85. else
  86. {
  87. hdnText.Value = yourValue;
  88. ListViewControlBind(yourValue);
  89. }
  90. }
  91. }
Output Chamber

On initial load:

On initial load
Figure 4: On Initial Load

On page click:

On page click
Figure 5: On page click

On second link click:

ListView
Figure 6: ListView

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