您的位置:首页 > 其它

新建页面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!");

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐