使用XmlDocument将dropdownlist中显示的书名的其它信息读入到相应的文本框中。添加 :删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能(XmlDocument)

-----------前台:-------------------
<body>
    <form id="form1" runat="server">
    <div>
   
    &nbsp;书名:
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        &nbsp;
        <asp:Button ID="btnShow" runat="server" Text="显示详细信息" Height="23px"
            οnclick="btnShow_Click" Width="90px" />
        &nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnDelete" runat="server" οnclick="btnDelete_Click" Text="删除" />
        <br />
        <br />
        <table style="width:40%;">
            <tr>
                <td>
                    <table style="width:100%;">
                        <tr>
                            <td>
                                作者:</td>
                            <td>
                                <asp:TextBox ID="txtAuthor" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                出版社:</td>
                            <td>
                                <asp:TextBox ID="txtPublisher" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                出版年月:</td>
                            <td>
                                <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Isbn号:</td>
                            <td>
                                <asp:TextBox ID="txtIsbn" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                价格:</td>
                            <td>
                                <asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                书名:</td>
                            <td>
                                <asp:TextBox ID="txtBookName" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Button ID="btnEdit" runat="server" Text="修改并保存" οnclick="btnEdit_Click"
                                    style="height: 21px" />
                            </td>
                            <td>
                                <asp:Button ID="btnAdd" runat="server" Text="添加" οnclick="btnAdd_Click" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>

-------------后台:------------------
 XmlDocument xdoc;
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                xdoc = new XmlDocument();
                xdoc.Load(Server.MapPath("books.xml"));
                XmlNodeList list = xdoc.GetElementsByTagName("name");
                foreach (XmlNode node in list)
                {
                    this.DropDownList1.Items.Add(node.InnerText);
                }
                Session["doc"] = xdoc;
            }
            else
            {
                xdoc = Session["doc"] as XmlDocument;
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
          
            XmlNode node = xdoc.DocumentElement.SelectSingleNode("book[name='" + this.DropDownList1.Text + "']");
            foreach (XmlNode item in node.ChildNodes)
            {
                if (item.LocalName == "author")
                {
                    this.txtAuthor.Text = item.InnerText;
                }
                else if (item.LocalName == "publisher")
                {
                    this.txtPublisher.Text = item.InnerText;
                }
                else if (item.LocalName == "date")
                {
                    this.txtDate.Text = item.InnerText;
                }
                else if (item.LocalName == "isbn")
                {
                    this.txtIsbn.Text = item.InnerText;
                }
                else if (item.LocalName == "price")
                {

                    this.txtPrice.Text = item.InnerText;
                }

            }
 
      
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            XmlNode node = xdoc.DocumentElement.SelectSingleNode("book[name='" + this.DropDownList1.Text + "']");
            xdoc.DocumentElement.RemoveChild(node);
            this.DropDownList1.Items.RemoveAt(this.DropDownList1.SelectedIndex);
            xdoc.Save(Server.MapPath("books.xml"));
            this.Response.Write("删除成功!");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            XmlNode node = xdoc.DocumentElement.SelectSingleNode("book[name='" + this.DropDownList1.Text + "']");
            node.SelectSingleNode("author").InnerText = this.txtAuthor.Text;
            node.SelectSingleNode("publisher").InnerText = this.txtPublisher.Text;
            node.SelectSingleNode("date").InnerText = this.txtDate.Text;
            node.SelectSingleNode("isbn").InnerText = this.txtIsbn.Text;
            node.SelectSingleNode("price").InnerText = this.txtPrice.Text;
            xdoc.Save(Server.MapPath("books.xml"));
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key1", "<script>alert('save ok!')</script>");
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            XmlElement book = xdoc.CreateElement("book");
            XmlElement au = xdoc.CreateElement("author");
            au.AppendChild(xdoc.CreateTextNode(this.txtAuthor.Text));
            XmlElement pu = xdoc.CreateElement("publisher");
            pu.AppendChild(xdoc.CreateTextNode(this.txtPublisher.Text));
            XmlElement pr = xdoc.CreateElement("price");
            pr.AppendChild(xdoc.CreateTextNode(this.txtPrice.Text));
            XmlElement da = xdoc.CreateElement("date");
            da.AppendChild(xdoc.CreateTextNode(this.txtDate.Text));
            XmlElement isb = xdoc.CreateElement("isbn");
            isb.AppendChild(xdoc.CreateTextNode(this.txtIsbn.Text));
            XmlElement na = xdoc.CreateElement("name");
            na.AppendChild(xdoc.CreateTextNode(this.txtBookName.Text));
            book.AppendChild(au);
            book.AppendChild(pu);
            book.AppendChild(pr);
            book.AppendChild(da);
            book.AppendChild(isb);
            book.AppendChild(na);
            xdoc.DocumentElement.AppendChild(book);
            this.DropDownList1.Items.Add(this.txtBookName.Text);
            xdoc.Save(Server.MapPath("books.xml"));
            this.ClientScript.RegisterClientScriptBlock(this.GetType(),"key3", "<script>alert('append ok!');</script>");
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值