运用gridview显示xml文件并能增删改查

前台代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
            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>
    </div>
    <hr />
    <div>
       书名:<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
       <asp:Button ID="btnSearch" runat="server" Text="查找" οnclick="btnSearch_Click"></asp:Button>
       <%--<asp:DropDownList ID="DropDownList1" runat="server">
       
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="显示详细信息"  />
        <asp:Button ID="btndelete" runat="server"  Text="删除" />--%>
        <br />
        <table style="width:50%;">
            <tr>
                <td class="style1">
                    作者:</td>
                <td>
                    <asp:TextBox ID="txtauthor" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    出版社:</td>
                <td>
                    <asp:TextBox ID="txtpublisher" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    出版年月:</td>
                <td>
                    <asp:TextBox ID="txtdate" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    Isbu号:</td>
                <td>
                    <asp:TextBox ID="txtisbn" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    价格:</td>
                <td>
                    <asp:TextBox ID="txtprice" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    <asp:Button ID="btnadd" runat="server"  Text="添加" />
                </td>
                <td>
                    书名:<asp:TextBox ID="txtbookname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    <asp:Button ID="Button2" runat="server"  Text="修改并保存" οnclick="Button2_Click" />
                </td>
                <td>
                    <asp:Button ID="Button3" runat="server" οnclick="Button3_Click" Text="返回到XML" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

后台代码

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 XML
{
    public partial class XmlAndDataset : 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;
                bindXML();
            }
            else
            {
                ds = Session["ds"] as DataSet;
            }
          
        }

        private void bindXML()
        {
            this.GridView1.DataSource = ds.Tables[0];
            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 btnSearch_Click(object sender, EventArgs e)
        {
            ds.Tables[0].DefaultView.RowFilter = "name like '%"+this.txtName+"%'";
            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.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 Button2_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 Button3_Click(object sender, EventArgs e)
        {
            //把数据返回到XML文件中
            //ds.WriteXml(Server.MapPath("books4.xml"));
            ds.WriteXml(Server.MapPath("books4.xml"), XmlWriteMode.WriteSchema);
            ds.WriteXmlSchema(Server.MapPath("aaa.xml"));
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "aaa", "<script>alert('保存到books4中')</script>");
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值