<< Click to Display Table of Contents >> ListView - ItemType (4.5+) |
![]() ![]() ![]() |
A partir da versão 4.5 do ASP.NET existe uma nova propriedade para ListView chamada ItemType.
Ela permite otimizar o aceso a dados para não precisar usar o Eval() ou Bind().
Fonte aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Help.Default" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Market Automações</title>
<link rel="shortcut icon" type="image/x-icon" href="http://market.com.br/img/favicon.ico" />
<link rel="icon" type="image/x-icon" href="http://market.com.br/img/favicon.ico" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style>
.img {
margin-top: 40px;
margin-bottom: 40px;
}
.table tbody tr:hover td {
background-color: yellow;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<asp:Image ID="img" ImageUrl="http://market.com.br/img/market_logo.png" CssClass="center-block img" runat="server" />
<asp:ListView ID="lv" runat="server" ItemType="Help.Pasta">
<LayoutTemplate>
<table class="table table-striped table-bordered table-condensed">
<tr>
<th>Nome
</th>
<th align="right">Tópicos
</th>
<th align="right">Imagens
</th>
</tr>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:HyperLink ID="hl" runat="server" Text='<%#:Item.Nome%>' NavigateUrl='<%#:Item.URL%>'></asp:HyperLink></td>
<td align="right">
<asp:Label ID="lbTopicos" runat="server" Text='<%#:Item.Topicos%>'></asp:Label></td>
<td align="right">
<asp:Label ID="lbImagens" runat="server" Text='<%#:Item.Imagens%>'></asp:Label></td>
</tr>
</ItemTemplate>
</asp:ListView>
</div>
</form>
</body>
</html>
Fonte cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Help
{
// esta classe será usado como "registro"
public class Pasta
{
public string Nome { get; set; }
public string URL { get; set; }
public int Topicos { get; set; }
public int Imagens { get; set; }
}
public partial class Default : System.Web.UI.Page
{
// uma coleção do objeto Pasta será fonte de dados da ListView
private List<Pasta> Pastas;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Pastas = new List<Pasta>();
string Raiz = Server.MapPath("~/");
DirectoryInfo dirinfo = new DirectoryInfo(Raiz);
foreach (DirectoryInfo dir in dirinfo.GetDirectories())
{
Pasta p = new Pasta();
p.Nome = dir.Name.Substring(0,1).ToUpper() + dir.Name.Substring(1);
p.URL = "~/" + dir.Name + "/index.html";
// conta imagens e htmls
DirectoryInfo dirAtual = new DirectoryInfo(dir.FullName);
p.Imagens = (from row in dirAtual.GetFiles("*.jpg") select row).Count();
p.Topicos = (from row in dirAtual.GetFiles("*.htm") select row).Count();
if (p.Topicos > 0 && p.Imagens > 0)
Pastas.Add(p);
}
Pastas.Sort(delegate (Pasta p1, Pasta p2) { return p1.Nome.CompareTo(p2.Nome); });
lv.DataSource = Pastas;
lv.DataBind();
}
}
}
}
Usando DataTable no lugar de List<Pessoa>
<asp:ListView ID="lv" runat="server" ItemType="System.Data.DataRowView">
<ItemTemplate>
<asp:HyperLink ID="hl" runat="server" Text='<%#:Item["Nome"]%>' NavigateUrl='<%#:Item["URL"]%>'></asp:HyperLink>
<asp:Label ID="lbTopicos" runat="server" Text='<%#:Decimal.Parse(Item["Salario"].ToString()).ToString("#,##0.00")%>'></asp:Label>
<asp:Label ID="lbImagens" runat="server" Text='<%#:DateTime.Parse(Item.Row["Nasc"].ToString()).ToString("dd-MMM-yy")%>'></asp:Label>
</ItemTemplate>
</asp:ListView>
</asp:ListView>