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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 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


    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

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek