asp.net 2.0+xml留言板
asp.net 2.0+xml留言板,实现排序,分页,添加功能!功能稍微简单,要不一天就搞不定了,没有用户头像,
本来是做了,但是取的时候出了点问题,下次有时间加把。
添加留言页面:WriteMessage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WriteMessage.aspx.cs"
Inherits="MyWeb.WriteMessage" %>
<!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">
body{font-size:12px}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="50%" style="margin-top:70px">
<tr>
<td>昵称:</td>
<td align="left"><asp:TextBox ID="txtName" runat="server"
MaxLength="20"></asp:TextBox></td>
</tr>
<tr>
<td>博客:</td>
<td align="left"><asp:TextBox ID="txtBlog" runat="server"
MaxLength="100"></asp:TextBox></td>
</tr>
<tr>
<td>E-Mail:</td>
<td align="left"><asp:TextBox ID="txtEmail" runat="server"
MaxLength="50"></asp:TextBox></td>
</tr>
<tr>
<td style="height: 58px">内容:</td>
<td align="left" style="height: 58px">
<asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine" Height="50px"
Width="300px"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnAdd" runat="server" Text="添 加" OnClick="btnAdd_Click"
/>
<asp:Button ID="btnCancel" runat="server" Text="重 置" OnClick="btnCancel_Click"
/>
<asp:Button ID="btnView" runat="server" Text="查 看" OnClick="btnView_Click" />
<asp:Label ID="lblMessage" runat="server" Text=""
ForeColor="Coral"></asp:Label></td>
</tr>
</table>
</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.Text.RegularExpressions;
using System.IO;
namespace MyWeb
{
public partial class WriteMessage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnView.Attributes.Add("onclick","return confirm('如果您填写了数据,数据将丢失,页面将
跳转到首页!');");
lblMessage.Text = string.Empty;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
string sName = txtName.Text.Trim();
string sBlog = txtBlog.Text.Trim();
string sEmail = txtEmail.Text.Trim();
string sContent = txtContent.Text.Trim();
string sXmlFile = Server.MapPath("Xml\\Message.xml");
if(CheckEmail(sEmail))
{
FileStream readStream = new FileStream(sXmlFile, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);
DataSet ds = new DataSet();
ds.ReadXml(readStream);
readStream.Close();
DataRow dataRow = ds.Tables[0].NewRow();
dataRow["name"] = sName;
dataRow["blog"] = sBlog;
dataRow["email"] = sEmail;
dataRow["content"] = sContent;
dataRow["replyTime"] = System.DateTime.Now;
ds.Tables[0].Rows.Add(dataRow);
FileStream writeStream = new FileStream(sXmlFile, FileMode.Open,
FileAccess.Write, FileShare.ReadWrite);
ds.WriteXml(writeStream);
writeStream.Close();
lblMessage.Text = "添加成功!";
ClearInput();
}
}
protected void btnView_Click(object sender, EventArgs e)
{
Response.Redirect("Message.aspx",true);
}
protected void btnCancel_Click(object sender, EventArgs e)
{
ClearInput();
}
#region 函数
private bool CheckEmail(string sEmail)
{
if (sEmail == string.Empty)
{
lblMessage.Text = "电子邮件地址不为空!";
return false;
}
if (!Regex.IsMatch(sEmail, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
{
lblMessage.Text = "电子邮件格式有误!";
return false;
}
return true;
}
private void ClearInput()
{
txtName.Text = string.Empty;
txtBlog.Text = string.Empty;
txtEmail.Text = string.Empty;
txtContent.Text = string.Empty;
}
#endregion
}
}
查看留言 Message.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Message.aspx.cs"
Inherits="MyWeb.Message" %>
<!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">
body{font-size:12px}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:Repeater ID="rptMain" runat="server">
<HeaderTemplate><table border="0" cellpadding="0" cellspacing="0"
width="70%"></HeaderTemplate>
<ItemTemplate>
<tr>
<!--左侧-->
<td>
<table id="Table1" border="0" bordercolor="#f0f8ff" cellpadding="0"
cellspacing="0" style="width: 150px;" runat="server">
<tr>
<td class="leftd" style="background:url
('image/property_back.gif') no-repeat center">
用户名:<%#DataBinder.Eval(Container.DataItem,"name") %>
</td>
</tr>
</table>
</td>
<!--右侧-->
<td valign="top">
<table border="1" bordercolor="#f0f8ff" cellpadding="0" cellspacing="0"
style="width: 720px; height:100%">
<tr>
<td valign="top" width="700px" colspan="2">
<table border="0" bordercolor="#f0f8ff" cellpadding="0"
cellspacing="0" width="700px">
<tr>
<td align="left">
<a target="_blank" href='<%#Eval("blog") %>'><img
src="image/blog.gif" border="0" /></a>
<a target="_blank" href='mailto:<%#Eval("email")
%>'><img src="image/email.gif" border="0" /></a>
</td>
</tr>
<tr>
<td>
<table cellpadding="0" cellspacing="0"
width="650px">
<tr>
<td style="width:19px; height:18px"><img
src="image/f1.gif" /></td>
<td colspan="2" style="width:550px;
height:18px; background:url('image/f2.gif')"></td>
<td style="width:21px; height:18px;"><img
src="image/f3.gif" /></td>
</tr>
<tr>
<td style="width:19px; height:100px;
background:url('image/f4.gif')"></td>
<td align="left" valign="top" colspan="2"
style="width:550px; background-color:#FFFFF1">
<%#DataBinder.Eval
(Container.DataItem, "content")%>
</td>
<td style="width:21px; height:100px;
background:url(image/f5.gif)"></td>
</tr>
<tr>
<td style="width:19px; height:52px"><img
src="image/f6.gif" /></td>
<td style="width:32px; height:52px;"><img
src="image/f8.gif" /></td>
<td align="left" style="height:52px;
width:570px; background:url('image/f7.gif')"></td>
<td style="width:21px; height:52px;"><img
src="image/f9.gif" /></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="left" colspan="2"><hr size="1px" color="" /></td>
</tr>
<tr>
<td align="left" height="20px" width="500px"><font
color="#ff6600">留言时间:</font><%#DataBinder.Eval(Container.DataItem,"replyTime") %></td>
<td align="right" style="padding-right:5px; height:20px;"><a
href="#"><img src="image/top.gif" border="0" /></a></td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
<table border="0" cellpadding="0" cellspacing="0" >
<tr>
<td style="height: 16px">
<asp:Label ID="lblPageCount" runat="server" Text=""></asp:Label>
<asp:HyperLink ID="hLnkFirst" runat="server" Text="首页"></asp:HyperLink>
<asp:HyperLink ID="hLnkPrevious" runat="server" Text="上一页"></asp:HyperLink>
<asp:HyperLink ID="hLnkNext" runat="server" Text="下一页"></asp:HyperLink>
<asp:HyperLink ID="hLnkLast" runat="server" Text="尾页"></asp:HyperLink>
<asp:Label ID="lblCounts" runat="server" Text=""></asp:Label>
<asp:LinkButton ID="lbtnMeg" runat="server" Text="我要留言"
OnClick="lbtnMeg_Click"></asp:LinkButton>
</td>
</tr>
</table>
</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.IO;
using System.Xml;
using System.Xml.XPath;
namespace MyWeb
{
public partial class Message : System.Web.UI.Page
{
private int Count;
protected void Page_Load(object sender, EventArgs e)
{
BindRpt();
}
private void BindRpt()
{
string sXmlFile = Server.MapPath("Xml\\Message.xml");
FileStream readStream = new FileStream(sXmlFile, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);
DataSet ds = new DataSet();
ds.ReadXml(readStream);
readStream.Close();
ds.Tables[0].DefaultView.Sort = "replyTime DESC";
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 3;
Count = ds.Tables[0].Rows.Count;
int currentPage = Convert.ToInt32(Request.QueryString["page"]);
if (currentPage <= 0)
currentPage = 1;
pds.CurrentPageIndex = currentPage - 1;
hLnkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + 1;
if (!pds.IsFirstPage) hLnkPrevious.NavigateUrl = Request.CurrentExecutionFilePath +
"?page=" + Convert.ToInt32(currentPage - 1);
if (!pds.IsLastPage) hLnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?
page=" + Convert.ToInt32(currentPage + 1);
hLnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + pds.PageCount;
lblCounts.Text = "总共有" + Count + "条留言";
lblPageCount.Text = "共有" + pds.PageCount + "页";
rptMain.DataSource = pds;
rptMain.DataBind();
}
protected void lbtnMeg_Click(object sender, EventArgs e)
{
Response.Redirect("WriteMessage.aspx");
}
}
}

浙公网安备 33010602011771号