Dynamically populate the list view control in asp.net

If you want to populate your list view in runtime according to query then here is the code.

Make changes in following files



<add name="vinConn" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\intradb.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />


Databaseconn.vb in app_code

Imports System.Data.Services

Imports System.Linq

Imports System.ServiceModel.Web

Imports Microsoft.VisualBasic

Imports System.Data

Imports System.Data.SqlClient

Public Class databaseconn

' TODO: replace [[class name]] with your data class name

' Inherits DataService(Of [[class name]])

Public Shared connection As New SqlConnection( System.Configuration.ConfigurationManager.ConnectionStrings("vinConn").ConnectionString

End Class

Dynamicdatabinding.vb in App_code

Imports System.Data.Services

Imports System.Linq

Imports System.ServiceModel.Web

Imports Microsoft.VisualBasic

Imports System.Web.HttpContext

Imports System.Data

Imports System.Data.SqlClient

Imports databaseconn

Public Shared Sub rebindListview(ByVal query As String, ByVal ListviewControl As ListView)

'Binds Paging/Sorting listView with data from the specified query

' Bind listView to current query & always store ur query into session("currentquery") before calling

' reason is whenever page indexed changed or sort.. then it will show data from currentquery



Dim sqlComm As SqlCommand

Dim sqlReader As SqlDataReader

Dim dt As New DataTable()

Dim dataTableRowCount As Integer


sqlComm = New SqlCommand(query, connection)

sqlReader = sqlComm.ExecuteReader()


dataTableRowCount = dt.Rows.Count

If dataTableRowCount > 0 Then

ListviewControl.DataSource = dt


End If


Catch e As Exception

'lblDebug.text = e.Message


End Try


End Sub


Imports System.Data

Imports System.Data.SqlClient

Imports Dynamicdatabinding

Imports databaseconn

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then

'this will be executed only first time any event happen on web page

' Clear user paging/sort

Session("CurrentPage") = 0

Session("SortExpression") = Nothing

GridViewSortDirection = "ASC"

' Set current query

Session("CurrentQuery") = "SELECT * from empdetail"

End If

'this will be executed every time any event happen on web page

''page parameter inputs.. "requestedpage"

rebindListview(Session("currentQuery"), lvMylist)

End Sub


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="departments.aspx.vb" Inherits="_Default" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title> LPHPP Intranet</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />



<form id="form1" runat="server">


<asp:ScriptManager ID="ScriptManager1" runat="server">



<asp:UpdatePanel ID="UpdatePanel1" runat="server">


<asp:ListView runat="server" ID="lvMylist" >


<br /> <br /> Employees <br /> <br />

<asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>



<%-- Data-bound content. --%>

<asp:Label ID="NameLabel" runat="server"

Text='<%#Eval("eid") %>' />










For every time to change listview dynamically just do this

change query and call rebind function

Session("CurrentQuery") = "SELECT * from empdetail where eid='009383'"

rebindListview(Session("currentQuery"), lvMylist)

- vinod k


Christian said…
Hire Dotnetnuke Developer Programmer
Hey the information provided by you is really very helpful for me. It is not that easy to provide such information without proper knowledge of the subject. I am new in this field and I am thankful to you for this amazing information. Keep up the good work.
shibathethinker said…
Thanks for the tips Vinod. It really helped after browsing through (not finding) a sea of web sites .. I finally found this - it helped solve my problem.

Most Viewed Post

solution for facebook static.ak.fbcdn.net waiting

Connect Sony XPLOD MEX-BT2500 Bluetooth CD Receiver with ur mobile

Crystal Report : Show data horizontally (Left to right) i.e. columns as rows