.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListView练习.aspx.cs" Inherits="ListView练习" %>
<!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>
<style type="text/css">
<span style="color:#cc0000;"> body,div,p,img,h1,h2,h3,h4,h5{ margin:0px; padding:0px;}</span>
.fl{ float:left;}
.fr{ float:right;}
.clr{ clear:both;}
h1{ padding:15px;border-bottom:1px solid gray;}
#div_listview{ width: 700px; }
#div_menu{ width:300px;}
.divItem{ border-bottom:1px dashed #ededed; margin-bottom:10px; padding:10px;}
.divItem img{ margin-right:10px; float:left;padding:3px; background-color:White; border:1px solid #eaeaea;}
.divItem h2{ font-family:微软雅黑; font-size:16px; color:#444; letter-spacing:1px;
margin:0px 10px 0px 10px;}
.divItem p{ font-size:12px;color:#888; margin-bottom:10px; margin:10px 10px 10px 10px;}
.divItem a{ text-decoration:none;float:right; color:Black; margin-bottom:10px;}
.divItem a:hover{ color:Blue; }
.divItem:hover{ background-color:Gray;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>新闻列表</h1>
<div id="div_listview" class="fl">
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="itemHolder" DataSourceID="AccessDataSource2">
<LayoutTemplate>
<div runat="server" id="itemHolder">
</div>
</LayoutTemplate>
<ItemTemplate>
<div class="divItem" id="a1" runat="server">
<img alt="" src='<%#Eval("news_picsrc","imgs/{0}") %>' />
<div class="fl">
<h2><%#Eval("news_ttl") %></h2>
<p><%#Eval("news_date","{0:D}") %></p>
<span style="color:#cc0000;"><a href="news_detail.aspx?nid=<%#Eval("news_id") %>">详细内容</a></span>
</div>
<div class ="clr"></div>
</div>
</ItemTemplate>
</asp:ListView>
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/mdb/mydb.mdb"
SelectCommand="SELECT * FROM [tb_news] WHERE ([news_cid] = ?)">
<SelectParameters>
<asp:SessionParameter DefaultValue="0" Name="news_cid" SessionField="CID"
Type="Int32" />
</SelectParameters>
</asp:AccessDataSource>
</div>
<div id="div_menu" class="fr">
<asp:BulletedList ID="BulletedList1" runat="server" DisplayMode="LinkButton"
DataSourceID="AccessDataSource1" DataTextField="lnc_name"
DataValueField="lnc_id" οnclick="BulletedList1_Click">
</asp:BulletedList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/mdb/mydb.mdb"
SelectCommand="SELECT [lnc_id], [lnc_name] FROM [list_newsclass]">
</asp:AccessDataSource>
</div>
<div class="clr"></div>
</div>
</form>
</body>
</html>
.aspx.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ListView练习 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BulletedList1_Click(object sender, BulletedListEventArgs e)
{
Session["CID"] = BulletedList1.Items[e.Index].Value;
}
}
效果如下: