使用XmlDocument将dropdownlist中显示的书名的其它信息读入到相应的文本框中

来源:互联网 发布:网络家教平台有哪些 编辑:程序博客网 时间:2024/06/05 06:35

Demo4.aspx文件:


<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 141px;
        }
        .style2
        {
            width: 200px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        书名:<asp:DropDownList ID="DropDownList1" runat="server" Height="25px"
            Width="184px">
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Xonclick="Button1_Click" Text="显示详情" />
        <br />
        <table style="width:100%;">
            <tr>
                <td class="style1">
                    作者:</td>
                <td class="style2">
                    <asp:TextBox ID="Txtauthor" runat="server" Width="199px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    出版社:</td>
                <td class="style2">
                    <asp:TextBox ID="Txtpublisher" runat="server" Width="198px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    出版年月:</td>
                <td class="style2">
                    <asp:TextBox ID="TxtYear" runat="server" Width="199px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    Isbn号:</td>
                <td class="style2">
                    <asp:TextBox ID="TxtISbn" runat="server" Width="199px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    价格:</td>
                <td class="style2">
                    <asp:TextBox ID="txtprice" runat="server" Width="199px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>

Demo4.aspx.cs文件:

        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 Button1_Click(object sender, EventArgs e)
        {
           
            XmlNode node = xdoc.DocumentElement.SelectSingleNode("book[name='" + this.DropDownList1.Text + "']");
            #region 方法1
            foreach (XmlNode item in node.ChildNodes)
            {

                if (item.LocalName == "author")
                {
                    this.Txtauthor.Text = item.InnerText;
                }
                if (item.LocalName == "publisher")
                {
                    this.Txtpublisher.Text = item.InnerText;
                }
                if (item.LocalName == "date")
                {
                    this.TxtYear.Text = item.InnerText;
                }
                if (item.LocalName == "isbn")
                {
                    this.TxtISbn.Text = item.InnerText;
                }
                if (item.LocalName == "price")
                {
                    this.txtprice.Text = item.InnerText;
                }
            }
            #endregion

            //#region 方法2
            //Txtauthor.Text = node.SelectSingleNode("author").InnerText;
            //Txtpublisher.Text = node.SelectSingleNode("publisher").InnerText;
            //TxtYear.Text = node.SelectSingleNode("date").InnerText;
            //TxtISbn.Text = node.SelectSingleNode("isbn").InnerText;
            //txtprice.Text = node.SelectSingleNode("price").InnerText;

            //#endregion
        }

原创粉丝点击