留言板网站
留言本网站 welcome.aspx
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
欢迎使用留言本
欢迎使用留言本,通过菜单进入对应的功能
这是本书中关于web网页卡发和设计的例子程序,他接受浏览者输入留言信息,并保存到数据库
同时管理者可以对留言者信息进行删除等操作
********************************************************************************* Welcome.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 Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
******************************************************************************** addliuyan.aspx
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
添加留言
">
">
通过该页面添加新留言到数据库,单击“添加”按钮添加!!!
BorderColor="#C0FFC0" BorderStyle="Ridge" Font-Bold="True" Font-Names="宋体" Font-Size="Larger" OnClick="btnAdd_Click" Width="112px" />
**************************************************************************** addliuyan.aspx.cs
using System;
using System.Data.SqlClient;
public partial class addliuyan : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{ //不是重新发送回来,则更新网页界面
this.tbName.Text = "";
this.tbTitle.Text = "";
this.tbContent.Text = "";
}
}
//添加留言
protected void btnAdd_Click(object sender, EventArgs e)
{
//更新提示信息
this.lbHint.Text = "";
bool ok = true;
string hint = "";
// 判断留言者的合法性
string name = this.tbName.Text.Trim();
if(string.IsNullOrEmpty(name))
{
ok = false;
hint = "留言者不能为空";
}
//判断留言主题合法性
string title = this.tbTitle.Text.Trim();
if (string.IsNullOrEmpty(title))
{
ok = false;
hint += "留言主题不能为空";
}
//判断留言内容合法性
string content = this.tbContent.Text.Trim();
if (string.IsNullOrEmpty(content))
{
ok = false;
hint += "留言内容不能为空";
}
//如果有数据不合法,提示并退出
if (!ok)
{
this.lbHint.Text = hint;
this.lbHint.ForeColor = System.Drawing.Color.Red;
return;
}
//保存数据到数据库
if (this.AddLiuYan(name, title, content))
{
//成功提示
this.lbHint.Text = "添加成功";
this.lbHint.ForeColor = System.Drawing.Color.Green;
this.tbName.Text = "";
this.tbTitle.Text = "";
this.tbContent.Text = "";
}
else
{
//失败提示
this.lbHint.Text = "添加失败";
this.lbHint.ForeColor = System.Drawing.Color.Red;
}
}
private bool AddLiuYan(string name, string title, string content)
{
//throw new NotImplementedException();
// try
// {
string constr = "Data Source=LIJINLIANG-PC\\SQLEXPRESS;Initial
Catalog=Liuyan;Integrated Security=True";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = " INSERT INTO Liuyanxinxi VALUES ('" + tbName.Text + " ','" +
DateTime.Now.ToString() + "', '" + tbTitle.Text + " ','" + tbContent.Text + " ');";
cmd.ExecuteNonQuery();
con.Close();
// }catch(Exception e)
// {
// Console.WriteLine(e.Message);
// }
return true;
}
}
*************************************************************************** viewliuyan.aspx
Inherits="viewliuyan" %>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
">
">
BorderColor="#C0C0FF" BorderStyle="Solid" BorderWidth="1px" Font-Size="X-Large" ForeColor="Green"
Height="30px" Onclick="btnView_Click" Width="108px" />
******************************************************************************* viewliuyan.aspx.cs
using System;
using System.Data.SqlClient;
public partial class viewliuyan : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnView_Click(object sender, EventArgs e)
{
//加载所有的留言信息
this.LoadLiuyan();
}
private void LoadLiuyan()
{
// throw new NotImplementedException();
string constr = "Data Source=LIJINLIANG-PC\\SQLEXPRESS;Initial Catalog=Liuyan;Integrated Security=True";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "SELECT * FROM Liuyanxinxi";
SqlDataReader dr = cmd.ExecuteReader();
System.Console.Write("\nLiuyanxinxi表达数据记录");
this.lbLiuyan.Text = "";
while (dr.Read())
{
System.Console.WriteLine();
/*
for (int index = 0; index
{
System.Console.Write(dr[index].ToString());
}
*/
string name = (string)dr["Liuyan_name"];
string time = (string)dr["Liuyan_time"];
string topic = (string)dr["Liuyan_topic"];
string content = (string)dr["Liuyan_content"];
this.ShowLiuyan(name, time, topic, content);
}
con.Close();
}
private void ShowLiuyan(string name, string time, string topic, string content)
{
// throw new NotImplementedException();
//生成留言信息显示的HTML文本段
string lyStr = string.Format("留言者:{0}留言时间:{1}标题:{2}内
容:{3}",name,time,topic,content);
//显示到界面
this.lbLiuyan.Text += lyStr;
}
}
**********************************************************************************