C#
  Home arrow C# arrow Page 4 - Creating an Online Shopping Cart and PayPa...
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 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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? 
C#

Creating an Online Shopping Cart and PayPal System
By: Tann San
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 28
    2006-06-13

    Table of Contents:
  • Creating an Online Shopping Cart and PayPal System
  • The GridView
  • The Page Declaration
  • An Important Function

  • 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


    Creating an Online Shopping Cart and PayPal System - An Important Function


    (Page 4 of 4 )

    Now is a good time to open the main class file titled "site.aspx.cs" inside the "cs" folder and locate the function called "gvBasket_RowDataBound."  Here's the function on its own, although you should check out the rest of the file in your own time:

      protected void gvBasket_RowDataBound(object sender, GridViewRowEventArgs e)
         {
            switch( e.Row.RowType )
               {
                  case DataControlRowType.DataRow:

                     // Name/Desc
                     ((Literal)e.Row.FindControl("litItemName")).Text = Convert.ToString(((DataRowView)e.Row.DataItem)["name"]);

                     // Quantity                          
                     string quantity = Convert.ToString(((DataRowView)e.Row.DataItem)["quantity"]);

                     ((HtmlInputText)e.Row.FindControl("itProductQuantity")).Value = quantity;

                     // Price                          
                     ((Literal)e.Row.FindControl("litPrice")).Text = String.Format("{0:C2}", Convert.ToDouble(((DataRowView)e.Row.DataItem)["price"]) * Convert.ToInt32(quantity));                                         
                     break;

                  case DataControlRowType.Footer:
                     DataTable dtShop = getBasketDt();

                     double total = 0.00;
                     for(int i = 0; i < dtShop.Rows.Count; i++)
                        {
                           total += Convert.ToInt32(dtShop.Rows[i]["quantity"]) * Convert.ToDouble(dtShop.Rows[i]["price"]);
                        }

                     ((Literal)e.Row.FindControl("litTotalQuantity")).Text = Convert.ToString(dtShop.Compute("SUM(quantity)", ""));

                     ((Literal)e.Row.FindControl("litDeliveryPrice")).Text = String.Format("{0:C2}", Convert.ToDouble(calcDeliveryCost(total)));

                     ((Literal)e.Row.FindControl("litTotalPrice")).Text = String.Format("{0:C2}", Convert.ToDouble(calcDeliveryCost(total)) + total);
                     break;
               }
         }

    The first thing we do is perform a switch on the RowType property so we can tell whether we are populating a Header, Footer or Item Template as all three will be separate calls to this same function. For both the product and basket we catch the DataControlRowType.DataRow row type as this is our ItemTemplate. 

    Because we gave all our controls unique IDs on the HTML page we can use the row's FindControl function. This will return an "Object" if any of the controls inside the row have a matching ID. We cast that to the type of object we're expecting, for example a "Literal" or a "HtmlInputText" field, and then assign the data to it via its TextorValue property. Each time a row is bound it gets passed into the function via the GridViewRowEventArgs.Row property. Using that we can access the row's DataItem, which contains all the data for that row from the DataSource.  It's then up to us to decide which data we want out of that and how we shall use it. 

    In the basket we pull the name, quantity and price columns out of the DataItem and assign them to the relevant controls we embedded within the ItemTemplate.  It's much the same for the DataControlRowType.Footer except that we pull a copy of the DataSource out of Session state ( getBasketDt(); ) since we want to generate the total cost and delivery cost using information from all of the rows, and not just the single row that got passed into the function.

    Conclusion

    Hopefully this article has shown you the basics of using a GridView as well as a few other tips and tricks along the way. We looked at one method of implementing the GridView and how we can have complete control over how its contents are generated.  The next installment in this series will look into where the data comes from and take you through setting up the actual product/basket page.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hey, I'm open to...
       · for some reason it wont work on my server do i have to have an asp server?
       · Hiya, you need to make sure you have the Microsoft .net Framework v1 and v2...
       · I am wondering if you could show an example of how to calculate the delivery costs...
     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT