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
- Binding Data Using ListView and GridView in ASP.NET without Database
- Binding Data in ListView on LinkButton Click
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:
- USE [ProductDb]
- GO
- /****** Object: Table [dbo].[Product] Script Date: 31.07.2015 10:39:45 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- SET ANSI_PADDING ON
- GO
- CREATE TABLE [dbo].[Product](
- [ProductId] [bigint] IDENTITY(1,1) NOT NULL,
- [CategoryId] [nchar](10) NULL,
- [ProductName] [varchar](50) NULL,
- [Qty] [int] NULL,
- [Price] [decimal](18, 2) NULL,
- [Description] [text] NULL,
- CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
- (
- [ProductId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
- GO
- SET ANSI_PADDING OFF
- GO
- ALTER TABLE [dbo].[Product] WITH CHECK ADD CONSTRAINT [FK_Product_Product] FOREIGN KEY([ProductId])
- REFERENCES [dbo].[Product] ([ProductId])
- GO
- ALTER TABLE [dbo].[Product] CHECK CONSTRAINT [FK_Product_Product]
- GO
The Table Product structure is here:
Figure 1: Tbl_Data
Query execution:
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:
- <style type="text/css">
- .linkCat {
- background-color:aqua;
- }
- </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:
- <div>
- <h3>ListView</h3>
- <%-- Linkbutton --%>
- <asp:LinkButton ID="lnkCat1" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Cat1" OnClick="ENameLinkBtn_Click" CommandArgument="Cat1"></asp:LinkButton>
- <asp:LinkButton ID="lnkCat2" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Cat2" OnClick="ENameLinkBtn_Click" CommandArgument="Cat2"></asp:LinkButton>
- <asp:LinkButton ID="lnkDefault" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Default" OnClick="ENameLinkBtn_Click" CommandArgument="Cat2"></asp:LinkButton>
- <asp:HiddenField ID="hdnText" runat="server" ClientIDMode="Static" Value="" />
- <%-- end --%>
- <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
- ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
- <LayoutTemplate>
- <table border="1">
- <tr>
- <th>Product
- </th>
- <th>Quantity
- </th>
- <th>Price
- </th>
- <th>Category
- </th>
- </tr>
- <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
- <tr>
- <td colspan="3">
- <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="2">
- <Fields>
- <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
- ShowNextPageButton="false" />
- <asp:NumericPagerField ButtonType="Link" />
- <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
- </Fields>
- </asp:DataPager>
- </td>
- </tr>
- </table>
- </LayoutTemplate>
- <GroupTemplate>
- <tr>
- <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
- </tr>
- </GroupTemplate>
- <ItemTemplate>
- <td>
- <%# Eval("ProductName") %>
- </td>
- <td>
- <%# Eval("Qty") %>
- </td>
- <td>
- <%# Eval("Price") %>
- </td>
- <td>
- <%# Eval("CategoryId") %>
- </td>
- </ItemTemplate>
- </asp:ListView>
- </div>
Now the design page looks as in the following.
Design Page
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListVieLinkWithDB.aspx.cs" Inherits="ListVieLinkWithDB" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>ListView Using DataBase</title>
- <style type="text/css">
- .linkCat {
- background-color: aqua;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h3>ListView</h3>
- <%-- Linkbutton --%>
- <asp:LinkButton ID="lnkCat1" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Cat1" OnClick="ENameLinkBtn_Click" CommandArgument="Cat1"></asp:LinkButton>
- <asp:LinkButton ID="lnkCat2" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Cat2" OnClick="ENameLinkBtn_Click" CommandArgument="Cat2"></asp:LinkButton>
- <asp:LinkButton ID="lnkDefault" CssClass="linkCat" runat="server" ClientIDMode="Static" Text="Default" OnClick="ENameLinkBtn_Click" CommandArgument="Cat2"></asp:LinkButton>
- <asp:HiddenField ID="hdnText" runat="server" ClientIDMode="Static" Value="" />
- <%-- end --%>
- <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
- ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
- <LayoutTemplate>
- <table border="1">
- <tr>
- <th>Product
- </th>
- <th>Quantity
- </th>
- <th>Price
- </th>
- <th>Category
- </th>
- </tr>
- <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
- <tr>
- <td colspan="3">
- <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="2">
- <Fields>
- <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
- ShowNextPageButton="false" />
- <asp:NumericPagerField ButtonType="Link" />
- <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
- </Fields>
- </asp:DataPager>
- </td>
- </tr>
- </table>
- </LayoutTemplate>
- <GroupTemplate>
- <tr>
- <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
- </tr>
- </GroupTemplate>
- <ItemTemplate>
- <td>
- <%# Eval("ProductName") %>
- </td>
- <td>
- <%# Eval("Qty") %>
- </td>
- <td>
- <%# Eval("Price") %>
- </td>
- <td>
- <%# Eval("CategoryId") %>
- </td>
- </ItemTemplate>
- </asp:ListView>
- </div>
- </form>
- </body>
- </html>

Figure 3: Design
Config Chamber
Add the following code to the Web.config.
- <connectionStrings>
- <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=true;Initial Catalog=ProductDb;" providerName="System.Data.SqlClient"/>
- </connectionStrings>
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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
Code behind page
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- public partial class ListVieLinkWithDB : System.Web.UI.Page
- {
- string connStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack) {
- ListViewControlBind("");
- }
- }
- protected void ListViewControlBind(string sGroup)
- {
- if (sGroup == "Cat1")
- sGroup = "1";
- if (sGroup == "Cat2")
- sGroup = "2";
- else
- sGroup = "";
- SqlConnection con = new SqlConnection(connStr);
- con.Open();
- string sQry = @"select ProductName,Qty,Price,CategoryId from dbo.Product";
- SqlCommand cmd = new SqlCommand(sQry, con);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- DataSet ds = new DataSet();
- da.Fill(dt);
- con.Close();
- ds.Tables.Add(dt);
- if (sGroup != "")
- {
- var dv = ds.Tables[0].DefaultView;
- dv.RowFilter = "CategoryId='" + sGroup + "'";
- DataSet ds1 = new DataSet();
- var newdt = dv.ToTable();
- ds1.Tables.Add(newdt);
- //bind data to data controls
- lvCustomers.DataSource = ds1.Tables[0];
- lvCustomers.DataBind();
- }
- else
- {
- lvCustomers.DataSource = ds.Tables[0];
- lvCustomers.DataBind();
- }
- }
- //paging code
- protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
- {
- (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
- if (hdnText.Value != "")
- {
- string yourValue = hdnText.Value.ToString();
- if (yourValue == "Default")
- {
- ListViewControlBind("");
- }
- else
- {
- this.ListViewControlBind(yourValue);
- }
- }
- else
- {
- this.ListViewControlBind("");
- }
- }
- protected void ENameLinkBtn_Click(object sender, EventArgs e)
- {
- LinkButton btn = (LinkButton)(sender);
- string yourValue = btn.CommandArgument;
- // do what you need here
- if (yourValue == "Default")
- {
- hdnText.Value = yourValue;
- ListViewControlBind("");
- }
- else
- {
- hdnText.Value = yourValue;
- ListViewControlBind(yourValue);
- }
- }
- }
On initial load:

Figure 4: On Initial Load
On page click:

Figure 5: On page click
On second link click:

Figure 6: ListView
I hope you liked this. Have a good day. Thank you for reading.

kalu singh raoPosted Jul 11, 2016, 2:37 AM
Nice...
Santhakumar MunuswamyPosted Jul 31, 2015, 3:01 PM
Good work. Thanks for sharing
Chervine BhiwooPosted Jul 31, 2015, 9:38 AM
Good one! Thanks
Vignesh ManiPosted Jul 31, 2015, 8:53 AM
Nice
Debendra DashPosted Jul 31, 2015, 8:53 AM
good one.......
Rajeesh MenothPosted Jul 31, 2015, 8:17 AM
Good Article Upendra Pratap Shahi
Pankaj Kumar ChoudharyPosted Jul 31, 2015, 7:00 AM
Nice Explain Sir..........
Nilesh JadavPosted Jul 31, 2015, 6:25 AM
Nice one sir