新建页面DataXML,使用GridView显示book.xm中的图书信息(将books.xml文档和dataSet交互。注意:DataView的使用。ds.Tables[0].defaultView

aspx

<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeBehind="Demo1.aspx.cs" Inherits="DataSetAndXML.Demo1" %>

<!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">
.style1
{
width: 174px;
}
.style2
{
width: 174px;
height: 52px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
Xonrowdeleting="GridView1_RowDeleting" Xonrowediting="GridView1_RowEditing" 
Xonselectedindexchanging="GridView1_SelectedIndexChanging" CellPadding="4" 
ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="name" HeaderText="书名" />
<asp:BoundField DataField="author" HeaderText="作者" />
<asp:BoundField DataField="publisher" HeaderText="出版社" />
<asp:BoundField DataField="data" HeaderText="日期" />
<asp:BoundField DataField="isbn" HeaderText="ISBN书号" />
<asp:BoundField DataField="price" HeaderText="价格" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
CommandName="Delete" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="Select" HeaderText="编辑" 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>
<hr />

书名:
<asp:DropDownList ID="DropDownList1" runat="server" Height="168px" 
Width="221px">
</asp:DropDownList>

<asp:Button ID="btnSearch" runat="server" Text="查找" Width="107px" 
Xοnclick="Button1_Click" />
<asp:Button ID="Button1" runat="server" Xοnclick="Button1_Click" 
Text="生成XML文档" />
<br />
<br />
<table style="height: 365px; width: 412px">
<tr><td class="style1">作者:</td><td class="style2">
<asp:TextBox ID="txtAuthor" runat="server"></asp:TextBox>
</td></tr>

<tr><td class="style1">出版社:</td><td class="style2">
<asp:TextBox ID="txtPblisher" runat="server"></asp:TextBox>
</td></tr>
<tr><td class="style1">出版年月:</td><td class="style2">
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
</td></tr> 
<tr><td class="style1">Isbn号::</td><td class="style2">
<asp:TextBox ID="txtIsbn" runat="server"></asp:TextBox>
</td></tr> 
<tr><td class="style1">价格:</td><td class="style2">
<asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
</td></tr> 
<tr><td class="style2">
书名:<br />
</td><td class="style2">
&nbsp;<asp:TextBox ID="txtName" runat="server" Width="106px"></asp:TextBox>
<br />
&nbsp;</td></tr> 
<tr><td class="style1">
<asp:Button ID="btnEdit" runat="server" Text="修改并保存" Xοnclick="btnEdit_Click" />
</td><td class="style2">
<asp:Button ID="btnAdd" runat="server" Text="添加" Height="26px" 
Xοnclick="btnAdd_Click"/>
</td></tr>

</table>
<br />

</form>
</body>
</html>

aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace DataSetAndXML
{
public partial class Demo1 : 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;
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
}
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.txtName.Text;
row["author"] = this.txtAuthor.Text;
row["publisher"] = this.txtPblisher.Text;
row["price"] = this.txtPrice.Text;
row["isbn"] = this.txtIsbn.Text;
row["data"] = this.txtDate.Text;
ds.Tables[0].Rows.Add(row);
this.bindXML();
}
protected void btnSearch_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].DefaultView.Delete(e.RowIndex);
//直接在DataTable中删除数据
ds.Tables[0].Rows.RemoveAt(e.RowIndex);
this.bindXML();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{

}

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
DataRowView row= ds.Tables[0].DefaultView[e.NewSelectedIndex];
this.txtName.Text=row["name"].ToString();
this.txtAuthor.Text = row["author"].ToString();
this.txtPblisher.Text = row["publisher"].ToString();
this.txtPrice.Text = row["price"].ToString();
this.txtIsbn.Text = row["isbn"].ToString();
this.txtDate.Text = row["data"].ToString();
}

protected void btnEdit_Click(object sender, EventArgs e)
{
int index = this.GridView1.SelectedIndex;
DataRowView row=ds.Tables[0].DefaultView[index];
row["name"] = this.txtName.Text;
row["author"] = this.txtAuthor.Text;
row["publisher"] =this.txtPblisher.Text;
row["price"]=this.txtPrice.Text;
row["isbn"]=this.txtIsbn.Text;
row["data"]=this.txtDate.Text;
this.bindXML();
}

protected void Button1_Click(object sender, EventArgs e)
{
ds.WriteXml(Server.MapPath("books.xml"), XmlWriteMode.IgnoreSchema);
//this.ClientScript.RegisterClientScriptBlock(this.GetType(), "aaa", "<script type='text/javascript'> alert('xml ready!');</script>");
ds.WriteXmlSchema(Server.MapPath("shema.xml"));
this.Response.Write("over!");
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值