SunQuest
 
       ASP.NET
  Home arrow ASP.NET arrow Page 4 - Order-Related Modules for an ASP.NET AJAX ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Order-Related Modules for an ASP.NET AJAX Server-Centric Based Online Shopping Website
By: Xianzhong Zhu
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-12-26

    Table of Contents:
  • Order-Related Modules for an ASP.NET AJAX Server-Centric Based Online Shopping Website
  • Behind the Scenes
  • More Behind the Scenes Code
  • Web Handler
  • Buying Goods
  • Viewing the Shopping Cart
  • Page Initialization

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Order-Related Modules for an ASP.NET AJAX Server-Centric Based Online Shopping Website - Web Handler


    (Page 4 of 7 )

     

    Here, we adopt the new ASP.NET 2.0 project-the newly-introduced web handler ".ashx" file. According to MSDN, the web handler ".ashx" file works just like an aspx file except WE are one step away from the messy browser level where HTML and C# mix. One reason we would write an .ashx file instead of an .aspx file is that our output is not going to a browser but to an XML-consuming client of some kind. As you have seen, the key piece lies in 'ImageUrl='<%# Eval("PictureID", "../Handler.ashx?Id={0}") %>''. And the following corresponds to the final form of it detected in the browser side.


    <img id="ProductView_ctl02_ProductPicture" style="border-width: 0px;
    height: 120px; width: 90px;" src="../Handler.ashx?Id=5"/>

    Now, let's take a further look into the code behind "Handler.ashx." Since the total code is short, without further ado, we list all of it:

    <%@ WebHandler Language="C#" Class="Handler" %>

    //using namespaces (omitted)

    public class Handler : IHttpHandler {

    public void ProcessRequest(HttpContext context){

    //context.Response.ContentType = "image/jpeg";

    context.Response.Cache.SetCacheability(HttpCacheability.Public);

    context.Response.BufferOutput = false;

    Stream outstream = null;

    int photoId = -1;

    if (context.Request.QueryString["Id"] != null &&
    context.Request.QueryString["Id"] != "")

    {

    photoId = Convert.ToInt32(context.Request.QueryString["Id"]);

    outstream = GetPhoto(photoId);

    }

    const int buffersize = 1024 * 16;

    byte[] buffer = new byte[buffersize];

    int count = outstream.Read(buffer, 0, buffersize);

    while (count > 0) {

    context.Response.OutputStream.Write(buffer, 0, count);

    count = outstream.Read(buffer, 0, buffersize);

    }

    }

    public Stream GetPhoto(int photoId) {

    SqlConnection myConnection = new SqlConnection
    (ConfigurationManager.ConnectionStrings
    ["SQLCONNECTIONSTRING"].ConnectionString);

    SqlCommand myCommand = new SqlCommand

    ("SELECT [Data] FROM [Pictures] WHERE [PictureID]=@PictureID",
    myConnection);

    myCommand.CommandType = CommandType.Text;

    myCommand.Parameters.Add(new SqlParameter("@PictureID", photoId));

    myConnection.Open();

    object result = myCommand.ExecuteScalar();

    try{

    return new MemoryStream((byte[])result);

    }

    catch (ArgumentNullException e) {

    return null;

    }

    finally{

    myConnection.Close();

    }

    }

    public bool IsReusable {

    get { return false; }

    }

    }

    In the above code, the core component is the public method, namelyProcessRequest. When the parameterized URL is passed to it, it accepts the parameter to a variable named "photoId," as follows.

    photoId = Convert.ToInt32(context.Request.QueryString["Id"]);

    Next, in the helper function GetPhoto, I directly use SQL operations to fetch the image data from the database (of course you can move the module elsewhere if you prefer). The subsequent lines of code in the ProcessRequest methodabstracts the binary image data and then writes it to the specified context, as follows:

    while (count > 0) {

    context.Response.OutputStream.Write(buffer, 0, count);

    count = outstream.Read(buffer, 0, buffersize);

    }

    The reason that we leveraged a whileloop is just to more efficiently deal with the data-you can follow your own calculating algorithms.


    As for the "product.aspx.cs" background file, there is nothing more particular to notice but the common data source binding operations, as follows.

    ProductView.DataSource = dr;

    ProductView.DataBind();

    So, you see, the .jpg (or .png, .gif) files stored in the database are finally displayed at the specified position on the web page!

    Author's Note: Here you should be careful to refer to the path of the ".ashx" files, or else you won't be able to see the image.

    More ASP.NET Articles
    More By Xianzhong Zhu


     

    ASP.NET ARTICLES

    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...
    - Back-end Management Tasks for an ASP.NET AJA...
    - User Information Management for an ASP.NET A...
    - Adding Comments and Search to an ASP.NET AJA...
    - Order-Related Modules for an ASP.NET AJAX Se...
    - User and Role Management for an ASP.NET AJAX...
    - Programming an ASP.NET AJAX Server-Centric B...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway