新建页面DataXML,使用GridView显示book.xm中的图书信息(将books.xml文档和dataSet交互。 )
--------前台:------------------------------
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowdeleting="GridView1_RowDeleting"
onselectedindexchanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:BoundField DataField="name" HeaderText="书名" />
<asp:BoundField DataField="author" HeaderText="作者" />
<asp:BoundField DataField="publisher" HeaderText="出版社" />
<asp:BoundField DataField="date" HeaderText="日期" />
<asp:BoundField DataField="isbn" HeaderText="ISBN书号" />
<asp:BoundField DataField="price" HeaderText="价格" />
<asp:CommandField ShowDeleteButton="True" />
<asp:ButtonField CommandName="Select" HeaderText="编辑" Text="按钮" />
</Columns>
</asp:GridView>
<hr />
<br />
书名:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnSerch" runat="server" Text="查找" onclick="btnSerch_Click" />
<asp:Button ID="btnBaoCun" runat="server" onclick="btnBaoCun_Click"
Text="生成XML文档" />
<%-- <asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="btnShow" runat="server" Text="显示详细信息" Height="23px"
onclick="btnShow_Click" Width="90px" />
<asp:Button ID="btnDelete" runat="server" Text="删除" />--%>
<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="修改并保存"
style="height: 21px" onclick="btnEdit_Click" />
</td>
<td>
<asp:Button ID="btnAdd" runat="server" Text="添加" onclick="btnAdd_Click" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
----------------------后台:-------------------------
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ds = new DataSet();
ds.ReadXml(Server.MapPath("books.xml"));
Session["ds"] = ds;
bindXML();
}
else
{
ds = Session["ds"] as DataSet;
}
}
private void bindXML()
{
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
DataRow row = ds.Tables[0].NewRow();
row["name"] = this.txtBookName.Text;
row["author"] = this.txtAuthor.Text;
row["publisher"] = this.txtPublisher.Text;
row["price"] = this.txtPrice.Text;
row["isbn"] = this.txtIsbn.Text;
row["date"] = this.txtDate.Text;
ds.Tables[0].Rows.Add(row);
this.bindXML();
}
protected void btnSerch_Click(object sender, EventArgs e)
{
ds.Tables[0].DefaultView.RowFilter = "name like '%" + this.txtName.Text + "%'";
this.bindXML();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
ds.Tables[0].Rows.RemoveAt(e.RowIndex);
this.bindXML();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
DataRowView row = ds.Tables[0].DefaultView[e.NewSelectedIndex];
this.txtBookName.Text = row["name"].ToString();
this.txtAuthor.Text = row["author"].ToString();
this.txtPublisher.Text = row["publisher"].ToString();
this.txtPrice.Text = row["price"].ToString();
this.txtIsbn.Text = row["isbn"].ToString();
this.txtDate.Text = row["date"].ToString();
}
protected void btnEdit_Click(object sender, EventArgs e)
{
int index = this.GridView1.SelectedIndex;
DataRowView row = ds.Tables[0].DefaultView[index];
row["name"] = this.txtBookName.Text;
row["author"] = this.txtAuthor.Text;
row["publisher"] = this.txtPublisher.Text;
row["price"] = this.txtPrice.Text;
row["isbn"] = this.txtIsbn.Text;
row["date"] = this.txtDate.Text;
this.bindXML();
}
protected void btnBaoCun_Click(object sender, EventArgs e)
{
ds.WriteXml(Server.MapPath("books.xml"),XmlWriteMode.WriteSchema);
ds.WriteXmlSchema(Server.MapPath("books1.xml"));
this.Response.Write("over!");
}