Dynamically populate the list view control in asp.net
Web.config
<connectionStrings>
<add name="vinConn" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\intradb.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
connectionStrings>
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
connection.Close()
connection.Open()
Dim sqlComm As SqlCommand
Dim sqlReader As SqlDataReader
Dim dt As New DataTable()
Dim dataTableRowCount As Integer
Try
sqlComm = New SqlCommand(query, connection)
sqlReader = sqlComm.ExecuteReader()
dt.Load(sqlReader)
dataTableRowCount = dt.Rows.Count
If dataTableRowCount > 0 Then
ListviewControl.DataSource = dt
ListviewControl.DataBind()
End If
sqlReader.Close()
Catch e As Exception
'lblDebug.text = e.Message
connection.Close()
End Try
connection.Close()
End Sub
Default.vb
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
Default.aspx
<%@ 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" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListView runat="server" ID="lvMylist" >
<LayoutTemplate>
<br /> <br /> Employees <br /> <br />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<%-- Data-bound content. --%>
<asp:Label ID="NameLabel" runat="server"
Text='<%#Eval("eid") %>' />
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
</form>
</body>
</html>
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
Comments
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.