新建页面DataXML,使用GridView显示book.xm中的图书信息(将books.xml文档和dataSet交互。注意:DataView的使用。ds.Tables[0].defaultView
2012-12-21 15:03
639 查看
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"
Xonclick="Button1_Click" />
<asp:Button ID="Button1" runat="server" Xonclick="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">
<asp:TextBox ID="txtName" runat="server" Width="106px"></asp:TextBox>
<br />
</td></tr>
<tr><td class="style1">
<asp:Button ID="btnEdit" runat="server" Text="修改并保存" Xonclick="btnEdit_Click" />
</td><td class="style2">
<asp:Button ID="btnAdd" runat="server" Text="添加" Height="26px"
Xonclick="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!");
}
}
}
<%@ 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"
Xonclick="Button1_Click" />
<asp:Button ID="Button1" runat="server" Xonclick="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">
<asp:TextBox ID="txtName" runat="server" Width="106px"></asp:TextBox>
<br />
</td></tr>
<tr><td class="style1">
<asp:Button ID="btnEdit" runat="server" Text="修改并保存" Xonclick="btnEdit_Click" />
</td><td class="style2">
<asp:Button ID="btnAdd" runat="server" Text="添加" Height="26px"
Xonclick="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!");
}
}
}
相关文章推荐
- 新建页面DataXML,使用GridView显示book.xm中的图书信息(将books.xml文档和dataSet交互。 )
- 在昨天作业的基础上添加 :删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能/新建页面DataXML,使用GridView显示book.xm中的图书信息
- 使用DataSet和DataView搜索XML文档
- 用户反馈信息textbox填充数据表,页面使用gridview显示反馈意见
- 使用XmlDocument将dropdownlist中显示的书名的其它信息读入到相应的文本框中
- 使用Aspose.Pdf置文档窗口和页面显示属性
- 使用Date日期对象来完成,在页面上根据不同时间显示不同的问候语,如:早上好,中午好,下午好,晚上好等信息
- 使用Jsoup抓取京东图书分类页面图书信息
- 使用spring webflow,在前台页面中如何显示后端返回的错误信息
- 使用XlmWrite把一个xml文件写入输出流中在(demo2.aspx运行时显示xml文档)
- cocos2d-x 3.0 使用Sax解析xml文档(解决中文显示问题)
- .Net有关问题。在GridView中添加了一个HyperLinkField,用来显示显示详细信息,请教跳转后的详细信息页面该怎样写代码
- flexpaper上传带中文名字的文档,在页面显示若出现404错误时,请在server.xml文件中进行编码utf-8
- NSIS ---使用nsDialogs创建自定义页面,并获取输入到控件中的内容保存到一个XML文档中
- 通过使用浏览器对象模型,输出当前浏览器窗口中打开的文档的URL信息,并将显示在窗口中。
- 使用PHP向Mysql数据库插入信息,页面中文内容显示正常,但在数据库里却是乱码,这个怎么解决?
- 使用XlmReader读books.xml中有几个book
- javaEE01-使用很HTML的排版标签编写“网站信息页面”,使用图片标签编写“图面显示页面”,使用列表标签编写“友情链接页面”,使用表格标签编写“首页”,框架标签表现“后台页面”
- 在非英文字符集的页面上,如果使用Ajax方式进行数据交互的话,就必须要注意保证前后端数据的统一编码,否则,很容易就出现乱码!
- cocos2d-x 3.0 使用Sax解析xml文档(解决中文显示问题)