Ajax+GridView+Xml的簡易留言薄

來源:互聯網
上載者:User

aspx檔案
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>無標題頁</title>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" CellPadding="0" ForeColor="#333333" GridLines="None" AllowPaging="True" AutoGenerateColumns="False" Width="100%" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5" BorderStyle="Solid" BorderWidth="1px">
<Columns>
<asp:TemplateField>
<HeaderStyle Width="100%" />
<ItemTemplate>
<table border="0" cellpadding="2" cellspacing="1">
<tr>
<td>使用者姓名:<%#Eval("name") %>留言時間:<%#Eval("sdate")%></td>
</tr>
<tr><td><hr width="100%" color="green"/></td></tr>
<tr><td>留言內容:<%#Eval("couent")%></td></tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Right" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
&nbsp; &nbsp;&nbsp;
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<table>
<tr><td style="width: 306px">姓名
<asp:TextBox ID="TextBox1" runat="server" Width="144px" ></asp:TextBox></td></tr>
<tr><td style="width: 306px; height: 77px"> 留言內容
<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" Width="223px" Height="109px"></asp:TextBox></td></tr>
<tr><td colspan="2"> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="提交" SkinID="o" /></td></tr>

</table>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
cs檔案
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;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("XMLFile.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet("my");
ds.ReadXml(Server.MapPath("XMLFile.xml"));//讀取xml檔案
DataRow dr = ds.Tables[0].NewRow();
dr["name"] = TextBox1.Text.ToString();
dr["couent"] = TextBox2.Text.ToString();
dr["sdate"] = DateTime.Now;
ds.Tables[0].Rows.Add(dr);
ds.WriteXml(Server.MapPath("XMLFile.xml"));//寫入xml檔案
Response.Redirect("default.aspx");

}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
}
xml檔案
<?xml version="1.0" standalone="yes"?>
<person>
<user>
<name>xiaoxiao</name>
<couent>xiaoxoao</couent>
<sdate>2006-11-14 21:06:14</sdate>
</user>
</person>
在此下載代碼http://www.cnblogs.com/Files/TeaGreen/liuyanbo.rar

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.