-------------Demo.aspx-----------------
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
onrowdeleting="GridView1_RowDeleting"
onselectedindexchanging="GridView1_SelectedIndexChanging">
<AlternatingRowStyle BackColor="White" />
<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" Text="编辑" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<br />
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
<asp:Button ID="Button3" runat="server" οnclick="Button3_Click" Text="查询" />
<asp:Button ID="Button5" runat="server" οnclick="Button5_Click"
Text="生成XML文档" />
<br />
<table style="width:100%;">
<tr>
<td class="style1">
作者</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
出版社</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
出版年月</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
isbn号</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
价格</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
书名</td>
<td>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
<asp:Button ID="Button4" runat="server" οnclick="Button4_Click" Text="修改并保存" />
</td>
<td>
<asp:Button ID="Button2" runat="server" Text="添加" οnclick="Button2_Click" />
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
------------------Demo.aspx.cs------------
public partial class Demo : System.Web.UI.Page
{
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ds = new DataSet();
ds.ReadXml(Server.MapPath("books.xml"));
Session["ds"] = ds;
bingXML();
}
else
{
ds = Session["ds"] as DataSet;
}
}
private void bingXML()
{
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
DataRow row = ds.Tables[0].NewRow();
row["name"] = this.TextBox6.Text;
row["author"] = this.TextBox1.Text;
row["publisher"] = this.TextBox2.Text;
row["date"] = this.TextBox3.Text;
row["isbn"] = this.TextBox4.Text;
row["price"] = this.TextBox5.Text;
ds.Tables[0].Rows.Add(row);
this.bingXML();
}
protected void Button3_Click(object sender, EventArgs e)
{
ds.Tables[0].DefaultView.RowFilter = "name like '%"+TextBox7.Text+"%'";
this.bingXML();
}
protected void Button4_Click(object sender, EventArgs e)
{
int index = this.GridView1.SelectedIndex;
DataRowView row=ds.Tables[0].DefaultView[index];
row["name"] = this.TextBox6.Text;
row["author"] = this.TextBox1.Text;
row["publisher"] = this.TextBox2.Text;
row["date"] = this.TextBox3.Text;
row["isbn"] = this.TextBox4.Text;
row["price"] = this.TextBox5.Text;
ds.Tables[0].Rows.Add(row);
this.bingXML();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
在视图中删除
//ds.Tables[0].DefaultView.Delete(e.RowIndex);(两种方法)
在DataTable中删除
ds.Tables[0].Rows.RemoveAt(e.RowIndex);
this.bingXML();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
DataRowView row=ds.Tables[0].DefaultView[e.NewSelectedIndex];//在GridView中选择新行的索引
this.TextBox1.Text = row["author"].ToString();
this.TextBox2.Text = row["publisher"].ToString();
this.TextBox3.Text = row["date"].ToString();
this.TextBox4.Text = row["isbn"].ToString();
this.TextBox5.Text = row["price"].ToString();
this.TextBox6.Text=row["name"].ToString();
}
protected void Button5_Click(object sender, EventArgs e)
{
ds.WriteXml(Server.MapPath("books.xml"));
// this.ClientScript.RegisterClientScriptBlock(this.GetType(),"sdf","<script type='text/javascript'>alter</script>");
ds.WriteXml(Server.MapPath("books2.xml"), XmlWriteMode.IgnoreSchema);
ds.WriteXmlSchema(Server.MapPath("books3.xml"));
}
}