Introduction:

Here we will explain how to use ListView in asp.net or what is ListView in asp.net or how to bind data to the ListView in asp.net with example.

Description:

The ASP.NET ListView control enables you to display the items columns and rows format. You can display items individually, or you can group them. It’s also support Data binding using the Data Source property, which allows you to bind to various objects, including ADO.NET datasets or data table or System.Collections.ArrayList object.

The ListView control displays data in a format that you define by using templates and styles. It is useful for  displays data in any repeating structure, it similar to the DataList and Repeater controls.it’s also allows sorting and paging.

Example:

ListView.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListView.aspx.cs" 
                                          Inherits="AspDotNet.ListView" %>
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="margin-bottom: 15px;"><b>ListView Control in Asp.Net</b></div>
        <asp:ListView ID="EmployeeListview" runat="server" GroupPlaceholderID="groupPlaceHolder1"
            ItemPlaceholderID="itemPlaceHolder1">
            <LayoutTemplate>
                <table cellpadding="0" cellspacing="0" style="width: 600px;">
                    <tr style="background-color: red; color: #250e0e; height: 30px;">
                        <th style="width: 80px; text-align: left;">EmployeeID
                        </th>
                        <th style="width: 150px; text-align: left;">EmployeeName
                        </th>
                        <th style="width: 280px; text-align: left;">Comment
                        </th>
                    </tr>
                    <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
                </table>
            </LayoutTemplate>
            <GroupTemplate>
                <tr>
                    <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
                </tr>
            </GroupTemplate>
            <ItemTemplate>
                <td style="background-color: #f2f2f2; border-bottom: 1px solid #000;">
                    <%# Eval("EmployeeID") %>
                </td>
                <td style="background-color: #f2f2f2; border-bottom: 1px solid #000;">
                    <%# Eval("EpmloyeeName") %>
                </td>
                <td style="background-color: #f2f2f2; border-bottom: 1px solid #000;">
                    <%# Eval("Comment") %>
                </td>
            </ItemTemplate>
        </asp:ListView>
    </form>
</body>
</html>


ListView.aspx.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace AspDotNet
{
    public partial class ListView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ArrayList list = new ArrayList();
                Employee emp = new Employee();
                emp.EmployeeID = "EMPID0001";
                emp.EpmloyeeName = "Johen Mark";
                emp.Comment = "Reference site about Lorem Ipsum," +
                "giving information on its origins, as well as a random Lipsum generator.";
                list.Add(emp);
 
                emp = new Employee();
                emp.EmployeeID = "EMPID0002";
                emp.EpmloyeeName = "Mahesh Patel";
                emp.Comment = "Referencesite about Lorem Ipsum," +
                "giving information on its origins, as well as a random Lipsum generator."; ;
                list.Add(emp);
 
                emp = new Employee();
                emp.EmployeeID = "EMPID0003";
                emp.EpmloyeeName = "Rohan Roy";
                emp.Comment = "Reference site about Lorem Ipsum," +
         "giving information on its origins, as well as a random Lipsum generator.";
                list.Add(emp);
 
                emp = new Employee();
                emp.EmployeeID = "EMPID0004";
                emp.EpmloyeeName = "Kamal Roy";
                emp.Comment = "Reference site about Lorem Ipsum," +
                "giving information on its origins, as well as a random Lipsum generator.";
                list.Add(emp);
 
                emp = new Employee();
                emp.EmployeeID = "EMPID0005";
                emp.EpmloyeeName = "Vijay pathak";
                emp.Comment = "Reference site about Lorem Ipsum," +
                "giving information on its origins, as well as a random Lipsum generator.";
                list.Add(emp);
 
                EmployeeListview.DataSource =list;
                EmployeeListview.DataBind();
            }
        }
        class Employee
        {
            public string EmployeeID { get; set; }
            public string EpmloyeeName { get; set; }
            public string Comment { get; set; }
        }
    }
}

When you run Above example code Output will be shown like below:



 I Hope this Article Will Help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.