Example for Gruoping Items Using ListView

Sandhyab
Posted by Sandhyab under ASP.NET category on | Points: 40 | Views : 2738
<%@ Page Language="C#" AutoEventWireup="true" Theme="ListView" CodeFile="Question5_GroupItems.aspx.cs" Inherits="ListView_groupItem" %>
<%@ Register Src="~/UserControl/Menu.ascx" TagPrefix="uc1" TagName="Menu" %>
<!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></title>
</head>

<body>
<center>
<form id="form1" runat="server">
<div>
<uc1:Menu runat="server" id="Menu1" />
<h2>ListView Grouping</h2>
<asp:ListView ID="ListView1" runat="server" GroupItemCount="2" ItemPlaceholderID="itemPlaceholder"
GroupPlaceholderID="groupPlaceholder">
<LayoutTemplate>
<table>
<tr id="groupPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<td id="itemPlaceholder" runat="server">
</td>
</tr>
<groupseparatortemplate>
<tr id="Tr1" runat="server">
<td colspan="2"><hr /></td>
</tr>
</groupseparatortemplate>
</GroupTemplate>
<ItemTemplate>

<td style="background-color:Green; color:White; border:1px; text-align:center;">
<b>AutoId:</b>
<asp:Label ID="AutoIdLabel" runat="server" Text=' <%# Eval("AutoId") %>' /><br />
<b>FirstName:</b>
<asp:Label ID="FirstNameLabel" runat="server" Text=' <%# Eval("FirstName") %>' /><br />
<b>LastName:</b>
<asp:Label ID="LastNameLabel" runat="server" Text=' <%# Eval("LastName") %>' /><br />
<b>Age:</b>
<asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' /><br />
<b>Active:</b>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Active") %>' /><br />
</td>
<%--</table>--%>
</ItemTemplate>
</asp:ListView>

<asp:Label ID="lblMessage" runat="server"></asp:Label>
</div>
</form>
</center>
</body>
</html>


Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class ListView_groupItem : System.Web.UI.Page
{
string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
PopulateData();
}
}

private void PopulateData()
{
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(_connStr))
{
try
{
string sql = "Select * from PersonDetails";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
ad.Fill(table);
}
}
}
catch (Exception e)
{
lblMessage.Text = "Error Occured" + e;
}
}
ListView1.DataSource = table;
ListView1.DataBind();
}

}

Comments or Responses

Login to post response