在Repeater中新闻标题后添加new和hot图标
前台代码:
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
<!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">
.Alternating td
{
color:blue ;
font-weight:bold;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<asp:Repeater ID="rpHot_New" runat="server"
onitemdatabound="rpHot_New_ItemDataBound" >
<ItemTemplate>
<tr>
<td>
<%#Eval("Title") %>
</td>
<td>
<asp:Literal ID="ltl_new_hot" runat="server"></asp:Literal>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="Alternating">
<td>
<%#Eval("Title") %>
</td>
<td>
<asp:Literal ID="ltl_new_hot" runat="server"></asp:Literal>
</td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
</table>
</div>
</form>
</body>
</html>
<!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">
.Alternating td
{
color:blue ;
font-weight:bold;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<asp:Repeater ID="rpHot_New" runat="server"
onitemdatabound="rpHot_New_ItemDataBound" >
<ItemTemplate>
<tr>
<td>
<%#Eval("Title") %>
</td>
<td>
<asp:Literal ID="ltl_new_hot" runat="server"></asp:Literal>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="Alternating">
<td>
<%#Eval("Title") %>
</td>
<td>
<asp:Literal ID="ltl_new_hot" runat="server"></asp:Literal>
</td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
</table>
</div>
</form>
</body>
</html>
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rpHot_NewBinds();
}
}
private void rpHot_NewBinds()
{
DataTable dt = new DataTable();
using (SqlConnection conn=DBHelper.GetConnection())
{
conn.Open();
SqlCommand comm = new SqlCommand("select * from Test_New_Hot order by id desc", conn);
SqlDataReader sdr=comm.ExecuteReader();
dt.Load(sdr);
}
rpHot_New.DataSource = dt;
rpHot_New.DataBind();
}
protected void rpHot_New_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Literal lil_New_Hot = e.Item.FindControl("ltl_new_hot") as Literal;
DataRowView dv = e.Item.DataItem as DataRowView;
String status = dv["status"].ToString();
if (status == "1")
{
lil_New_Hot.Text = "<img src='image/new.gif'/>";
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rpHot_NewBinds();
}
}
private void rpHot_NewBinds()
{
DataTable dt = new DataTable();
using (SqlConnection conn=DBHelper.GetConnection())
{
conn.Open();
SqlCommand comm = new SqlCommand("select * from Test_New_Hot order by id desc", conn);
SqlDataReader sdr=comm.ExecuteReader();
dt.Load(sdr);
}
rpHot_New.DataSource = dt;
rpHot_New.DataBind();
}
protected void rpHot_New_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Literal lil_New_Hot = e.Item.FindControl("ltl_new_hot") as Literal;
DataRowView dv = e.Item.DataItem as DataRowView;
String status = dv["status"].ToString();
if (status == "1")
{
lil_New_Hot.Text = "<img src='image/new.gif'/>";
//其实最好是把图片路径放到数据库里,如:
//if(status>0)
//lil_New_Hot.Text = "<img src="+imgIcon+"/>";
}
else if (status == "2")
{
lil_New_Hot.Text = "<img src='image/hot.gif'/>";
}
}
}
}
else if (status == "2")
{
lil_New_Hot.Text = "<img src='image/hot.gif'/>";
}
}
}
结果:
图标下载: