productslisting.aspx

来自「《ASP.NET 2.0 XML 高级编程(第3版)》 《ASP.NET 2.」· ASPX 代码 · 共 53 行

ASPX
53
字号
<%@ Page Language="C#" MasterPageFile="~/CommonMaster.master" Title="Products Listing" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Serialization" %>
<%@ Import Namespace="ProductsProxy" %>

<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        /* Synchronous Web Service Call approach 
        ProductsProxy.ProductsService obj = new ProductsProxy.ProductsService();
        int categoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);
        //Bind the Web service return value to Products GridView
        gridProducts.DataSource = obj.GetProductsByCategoryID(categoryID);
        gridProducts.DataBind();
         */
        /* Asynchronous Web Service call through the XML file generated by the Windows Service */
        int categoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);
        List<Product> prodList = new List<Product>();
        string xmlFilePath = @"C:\Projects\Wrox\Products.xml";
        XmlSerializer serializer = new XmlSerializer(typeof(Product[]));        
        TextReader reader = new StreamReader(xmlFilePath);        
        Product[] prodArray = (Product[])serializer.Deserialize(reader);
        //Loop through the array and retrieve the products with the matching category id
        for (int i = 0; i < prodArray.Length; i++)
        {
            Product temp = prodArray[i];
            //Only add the matching products to the new collection
            if (temp.CategoryID == categoryID)
                prodList.Add(temp);
        }
        reader.Close();
        gridProducts.DataSource = prodList;
        gridProducts.DataBind();        
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="BodyContent" Runat="Server">
    <asp:GridView id="gridProducts" style="Z-INDEX: 101; LEFT: 162px; POSITION: absolute; TOP: 147px" runat="server" Height="321px" width="529px" BorderColor="Black" cellpadding="4" Font-Names="Verdana" Font-Size="8pt" AutoGenerateColumns="False" ShowFooter="True">
		<FooterStyle ForeColor="Control" BackColor="ActiveCaptionText"></FooterStyle>
		<HeaderStyle BackColor="Gray"></HeaderStyle>		
		<RowStyle BackColor="Control"></RowStyle>
		<Columns>
			<asp:BoundField DataField="ModelNo" HeaderText="ModelNumber"></asp:BoundField>
			<asp:BoundField DataField="ModelName" HeaderText="ModelName"></asp:BoundField>
			<asp:BoundField DataField="Description" HeaderText="Description"></asp:BoundField>
			<asp:BoundField DataField="Price" HeaderText="Price"></asp:BoundField>
			<asp:HyperLinkField Text="Product Details" DataNavigateUrlFields="ProductID" DataNavigateUrlFormatString="productdetails.aspx?ProductID={0}" HeaderText="Show Details"></asp:HyperLinkField>
		</Columns>
	</asp:GridView>
</asp:Content>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?