发表留言模块
前台:
<%@ Page Language="C#" 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>
<strong>发表留言:</strong><br />
用户名:<asp:TextBox ID="name" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="name"
ErrorMessage="用户名不能为空"></asp:RequiredFieldValidator><br />
内容:<asp:TextBox ID="content" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="content"
ErrorMessage="内容不能为空"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="发表" />
<a href="admin.aspx">转到管理员登陆界面</a><br />
<br />
<asp:Repeater ID="MyRepeater" runat="server">
<ItemTemplate>
<table width="800" style="background-color:GrayText"><tr>
<td align="left">
<asp:Label ID="Label3" runat="server" Text="主题:"></asp:Label>
<%#DataBinder.eval_r(Container.DataItem,"content") %></td>
<td align="center">
<asp:Label ID="Label4" runat="server" Text="作者:"></asp:Label>
<%#DataBinder.eval_r(Container.DataItem,"name") %></td>
<td align="right">
<asp:Label ID="Label5" runat="server" Text="发表时间:"></asp:Label>
<%#DataBinder.eval_r(Container.DataItem,"clienttime") %></td></tr></table>
<table width="800"><tr><td align="center">
<asp:Label ID="Label1" runat="server" Text="站长回复:">
</asp:Label><%#DataBinder.eval_r(Container.DataItem,"admincontent") %></td>
<td align="right">
<asp:Label ID="Label2" runat="server" Text="回复时间:">
</asp:Label><%#DataBinder.eval_r(Container.DataItem,"admintime") %></td></tr></table>
<hr />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Repeater();
}
}
private void Repeater()
{
string str = "server=localhost;uid=sa;pwd=123;database=message;";
SqlConnection connection = new SqlConnection(str);
string sql = "select content,name,clienttime,admincontent,admintime from message order by clienttime desc";
SqlDataAdapter adapter = new SqlDataAdapter(sql,connection);
DataSet myset = new DataSet();
adapter.Fill(myset,"message");
connection.Open();
MyRepeater.DataSource = myset;
MyRepeater.DataBind();
connection.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = "server=localhost;uid=sa;pwd=123;database=message;";
SqlConnection connection = new SqlConnection(str);
string sql = string.Format("select * from author where clientname='{0}'",name.Text);
SqlCommand cmd = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader datareader = cmd.ExecuteReader();
if (datareader.Read())
{
insert();
}
else
{
Response.Write("<script language='javascript'>alert('无此用户名!请重新输入!')</script>");
name.Focus();
}
datareader.Close();
connection.Close();
}
private void insert()
{
string conn = "server=localhost;uid=sa;pwd=123;database=message;";
SqlConnection connection = new SqlConnection(conn);
string sql = string.Format("insert into message(content,name) values('{0}','{1}')",content.Text,name.Text);
SqlCommand cmd = new SqlCommand(sql,connection);
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
Repeater();
}
}
管理员模块:
前台:
<%@ Page Language="C#" CodeFile="admin.aspx.cs" Inherits="admin" %>
<!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>
<strong><span style="font-size: 14pt">管理员登陆<br />
</span></strong>姓名:
<asp:TextBox ID="author" runat="server"></asp:TextBox><br />
密码:
<asp:TextBox ID="passwd" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="登陆" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="转到用户发表留言界面" OnClick="Button2_Click" /><br />
<br />
<asp:Repeater Visible="false" ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<table width="900" style="background-color:GrayText"><tr>
<td align="left">
<asp:Label ID="id" runat="server" Text="编号:"></asp:Label>
<%# DataBinder.eval_r(Container.DataItem,"id")%>
</td>
<td align="left">
<asp:Label ID="Label3" runat="server" Text="主题:"></asp:Label>
<%#DataBinder.eval_r(Container.DataItem,"content") %></td>
<td align="center">
<asp:Label ID="Label4" runat="server" Text="作者:"></asp:Label>
<%#DataBinder.eval_r(Container.DataItem,"name") %></td>
<td align="right">
<asp:Label ID="Label5" runat="server" Text="发表时间:"></asp:Label>
<%#DataBinder.eval_r(Container.DataItem,"clienttime") %></td></tr></table>
<asp:Label ID="Label10" runat="server" Text="站长回复:">
</asp:Label><%#DataBinder.eval_r(Container.DataItem,"admincontent") %>
<asp:TextBox ID="insert" runat="server"></asp:TextBox>
<asp:Button ID="ins" runat="server" CommandName="add" CommandArgument='<%# eval_r("id") %>' Text="更改站长回复" />
<table width="900"><tr>
<td align="right">
<asp:Label ID="Label2" runat="server" Text="回复时间:">
</asp:Label><%#DataBinder.eval_r(Container.DataItem,"admintime") %></td></tr></table>
<hr />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Repeater();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string ttt = "server=localhost;uid=sa;pwd=123;database=message;";
SqlConnection connection = new SqlConnection(ttt);
string sql = string.Format("select * from author where adminname='{0}' and adminpasswd='{1}'",author.Text, passwd.Text);
SqlCommand cmd = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Repeater1.Visible = true;
}
else
{
Response.Write("<script language='javascript'>alert('登录失败!')</script>");
}
dr.Close();
}
private void Repeater()
{
string str = "server=localhost;uid=sa;pwd=123;database=message;";
SqlConnection connection = new SqlConnection(str);
string sql = "select id,content,name,clienttime,admincontent,admintime from message order by clienttime desc";
SqlDataAdapter adapter = new SqlDataAdapter(sql,connection);
DataSet myset = new DataSet();
adapter.Fill(myset,"message");
connection.Open();
Repeater1.DataSource = myset;
Repeater1.DataBind();
connection.Close();
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.ToString() == "add")
{
string text = ((TextBox)e.Item.FindControl("insert")).Text;
int id = int.Parse(e.CommandArgument.ToString());
string conn = "server=localhost;uid=sa;pwd=123;database=message;";
SqlConnection connection = new SqlConnection(conn);
string sql = string.Format("update message set admincontent='{0}' where id={1}",text,id);
SqlCommand cmd = new SqlCommand(sql, connection);
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
Repeater();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
}
<script type="text/javascript" id="wumiiRelatedItems"> </script>