前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand">
<HeaderTemplate>我想说两句<hr /><table style="border:2px red solid; width:400px; height:1000px; position:relative; margin:0px auto"></HeaderTemplate>
<ItemTemplate>
<tr>
<td style="border:2px red solid;">昵称:<%#Eval("Userme") %></td>
<td style="border:2px red solid;">这是<%#Eval("Userme") %>于[<%#Eval("time") %>]的留言</td>
</tr>
<tr>
<td style="border:2px red solid;">
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("photo") %>' Width="45px" Height="60px" /></td>
<td style="border:2px red solid;"><%#Eval("contend")%><hr /><%#Eval("Userbey")%></td>
</tr>
<tr>
<td style="border:2px red solid;">ID:<%#Eval("id") %></td>
<td style="border:2px red solid;">
<asp:LinkButton ID="LinkButton1" runat="server" Text="给他发邮件" CommandName="1">给他发邮件</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" Text="删除留言" CommandName="Delete" CommandArgument='<%# Eval("id") %>'>删除留言</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" Text="回复留言" CommandName="Reply">回复留言</asp:LinkButton>
</td>
</tr></ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</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.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindLiuyanban();
}
private void BindLiuyanban()
{
string str = ConfigurationManager.ConnectionStrings["strcnn"].ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection(str))
{
using (SqlDataAdapter da = new SqlDataAdapter("select * from liuyan", sqlCnn))
{
DataSet ds = new DataSet();
da.Fill(ds);
this.Repeater1.DataSource = ds;
this.Repeater1.DataBind();
}
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
string str = ConfigurationManager.ConnectionStrings["strcnn"].ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection(str))
{
using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
{
sqlCnn.Open();
sqlCmm.CommandText = "delete from liuyan where id=" + e.CommandArgument.ToString();
sqlCmm.ExecuteNonQuery();
}
}
this.BindLiuyanban();
}
else if (e.CommandName == "Reply")
{
Response.Redirect("Default2.aspx");
}
else if (e.CommandArgument=="1")
{
Response.Redirect("Default2.aspx");
}
}
}