topical media & game development

talk show tell print

mashup-amazon-07-07-01-AmazonHTML-default.aspx.cs / cs



  using System;
  using System.Data;
  using System.Configuration;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  using com.amazon.webservices;
  using System.Xml.Serialization;
  using System.IO;
  
  public partial class _default : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
      }
  
      string SearchAmazon(string strKeywords, string strSearchIndex)
      {
          // Create a new instance of the proxy class
          AWSECommerceService myProxy = new AWSECommerceService();
  
          // Create a new instance of the ItemSearch class
          ItemSearch mySearch = new ItemSearch();
  
          // ItemSearchRequest stores the actual request parameters
          ItemSearchRequest mySearchRequest = new ItemSearchRequest();
  
          // Set some parameters, Keyword and Search Index
          mySearchRequest.Keywords = strKeywords;
          mySearchRequest.SearchIndex = strSearchIndex;        
  
          // Just need Small results, not the full enchilada
          mySearchRequest.ResponseGroup = new string[] { "Medium", "Request"};
  
          // Set the subscription and associate tags here
          mySearch.AWSAccessKeyId = ConfigurationManager.AppSettings["AWSAccessKeyId"];
          mySearch.AssociateTag = ConfigurationManager.AppSettings["AssociateTag"];
  
          // Setup request
          mySearch.Request = new ItemSearchRequest[] { mySearchRequest };
  
          // Execute the request and get the response
          ItemSearchResponse myResponse = myProxy.ItemSearch(mySearch);
  
          // Parse the response and return the results in HTML
          string strHTML = "";
          foreach (Items myItems in myResponse.Items)
          {
              foreach (Item myItem in myItems.Item)
              {
                  // Get the results in HTML
                  strHTML += FormatAsHTML(myItem);
              }
          }
          return strHTML;
      }
  
  
<summary> Returns HTML for a specific Amazon Item </summary> <param name="myItem">The Item instance</param> <returns>a string of HTML</returns> string FormatAsHTML(Item myItem) { // Will contain the results string strHTML ="";

          // Check for null
          if (myItem != null)
          {
              // Begin the item HTML
              strHTML += "<div class=\"item\">";
              // Add the title
              strHTML += "<h2>" + myItem.ItemAttributes.Title + "</h2>";
  
              // Add the item image if present
              if (myItem.SmallImage != null)
              {
                  strHTML += "<img src=" + myItem.SmallImage.URL;
                  strHTML += " width=" + myItem.SmallImage.Width;
                  strHTML += " height=" + myItem.SmallImage.Height;
                  strHTML += " align=\"left\" >";
              }
  
              // Add the ASIN
              strHTML += "<b>ASIN:</b> " + myItem.ASIN + "<br/>";
  
              // Add the price if present
              if (myItem.OfferSummary != null)
              {
                  if (myItem.OfferSummary.LowestNewPrice != null)
                  {
                      strHTML += "<b>Lowest Price:</b> " +
                         myItem.OfferSummary.LowestNewPrice.FormattedPrice
                         + "<br/>";
                  }
              }
              // Add a link to the details
              strHTML += "<a href=\"" + myItem.DetailPageURL + "\">View Details</a><br/>";
              
              // Close the output
              strHTML += "</div>";
          }
          return strHTML;
      }
  
      protected void cmdSubmit_ServerClick(object sender, EventArgs e)
      {
          string strKeywords = Request.Params["keywords"];
          string strSearchIndex = Request.Params["searchIndex"];
          myResults.Text = SearchAmazon(strKeywords, strSearchIndex);
      }
  }


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.