-----------前台:-------------------
<body>
<form id="form1" runat="server">
<div>
书名:
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="btnShow" runat="server" Text="显示详细信息" Height="23px"
οnclick="btnShow_Click" Width="90px" />
<asp:Button ID="btnDelete" runat="server" οnclick="btnDelete_Click" Text="删除" />
<br />
<br />
<table style="width:40%;">
<tr>
<td>
<table style="width:100%;">
<tr>
<td>
作者:</td>
<td>
<asp:TextBox ID="txtAuthor" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
出版社:</td>
<td>
<asp:TextBox ID="txtPublisher" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
出版年月:</td>
<td>
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Isbn号:</td>
<td>
<asp:TextBox ID="txtIsbn" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
价格:</td>
<td>
<asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
书名:</td>
<td>
<asp:TextBox ID="txtBookName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnEdit" runat="server" Text="修改并保存" οnclick="btnEdit_Click"
style="height: 21px" />
</td>
<td>
<asp:Button ID="btnAdd" runat="server" Text="添加" οnclick="btnAdd_Click" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
-------------后台:------------------
XmlDocument xdoc;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
xdoc = new XmlDocument();
xdoc.Load(Server.MapPath("books.xml"));
XmlNodeList list = xdoc.GetElementsByTagName("name");
foreach (XmlNode node in list)
{
this.DropDownList1.Items.Add(node.InnerText);
}
Session["doc"] = xdoc;
}
else
{
xdoc = Session["doc"] as XmlDocument;
}
protected void btnShow_Click(object sender, EventArgs e)
{
XmlNode node = xdoc.DocumentElement.SelectSingleNode("book[name='" + this.DropDownList1.Text + "']");
foreach (XmlNode item in node.ChildNodes)
{
if (item.LocalName == "author")
{
this.txtAuthor.Text = item.InnerText;
}
else if (item.LocalName == "publisher")
{
this.txtPublisher.Text = item.InnerText;
}
else if (item.LocalName == "date")
{
this.txtDate.Text = item.InnerText;
}
else if (item.LocalName == "isbn")
{
this.txtIsbn.Text = item.InnerText;
}
else if (item.LocalName == "price")
{
this.txtPrice.Text = item.InnerText;
}
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
XmlNode node = xdoc.DocumentElement.SelectSingleNode("book[name='" + this.DropDownList1.Text + "']");
xdoc.DocumentElement.RemoveChild(node);
this.DropDownList1.Items.RemoveAt(this.DropDownList1.SelectedIndex);
xdoc.Save(Server.MapPath("books.xml"));
this.Response.Write("删除成功!");
}
protected void btnEdit_Click(object sender, EventArgs e)
{
XmlNode node = xdoc.DocumentElement.SelectSingleNode("book[name='" + this.DropDownList1.Text + "']");
node.SelectSingleNode("author").InnerText = this.txtAuthor.Text;
node.SelectSingleNode("publisher").InnerText = this.txtPublisher.Text;
node.SelectSingleNode("date").InnerText = this.txtDate.Text;
node.SelectSingleNode("isbn").InnerText = this.txtIsbn.Text;
node.SelectSingleNode("price").InnerText = this.txtPrice.Text;
xdoc.Save(Server.MapPath("books.xml"));
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key1", "<script>alert('save ok!')</script>");
}
protected void btnAdd_Click(object sender, EventArgs e)
{
XmlElement book = xdoc.CreateElement("book");
XmlElement au = xdoc.CreateElement("author");
au.AppendChild(xdoc.CreateTextNode(this.txtAuthor.Text));
XmlElement pu = xdoc.CreateElement("publisher");
pu.AppendChild(xdoc.CreateTextNode(this.txtPublisher.Text));
XmlElement pr = xdoc.CreateElement("price");
pr.AppendChild(xdoc.CreateTextNode(this.txtPrice.Text));
XmlElement da = xdoc.CreateElement("date");
da.AppendChild(xdoc.CreateTextNode(this.txtDate.Text));
XmlElement isb = xdoc.CreateElement("isbn");
isb.AppendChild(xdoc.CreateTextNode(this.txtIsbn.Text));
XmlElement na = xdoc.CreateElement("name");
na.AppendChild(xdoc.CreateTextNode(this.txtBookName.Text));
book.AppendChild(au);
book.AppendChild(pu);
book.AppendChild(pr);
book.AppendChild(da);
book.AppendChild(isb);
book.AppendChild(na);
xdoc.DocumentElement.AppendChild(book);
this.DropDownList1.Items.Add(this.txtBookName.Text);
xdoc.Save(Server.MapPath("books.xml"));
this.ClientScript.RegisterClientScriptBlock(this.GetType(),"key3", "<script>alert('append ok!');</script>");
}