Documentation for: ASP.NET Ajax Version 1.0

This documentation is for a previous version. For the current released version, see the ASP.NET Ajax documentation on MSDN.

 

 

 

 

 

 

 

Skip Navigation Links.
cs
App_Code
ProductsView.cs
ShowProducts.aspx
images
awc_jersey_female_large.gif
awc_jersey_male_large.gif
awc_tee_female_large.gif
awc_tee_male_large.gif
awc_tee_male_yellow_large.gif
bikepump_large.gif
bike_lock_large.gif
bike_shoes_large.gif
bike_shorts_female_large.gif
bike_shorts_male_large.gif
chain_large.gif
chain_lube_large.gif
clipless_pedals_large.gif
co2_4tire_large.gif
double_headlight_large.gif
fork_large.gif
frame_black_large.gif
frame_blue_large.gif
frame_large.gif
frame_red_large.gif
frame_silver_large.gif
frame_yellow_large.gif
handlebar_large.gif
handpump_large.gif
hotrodbike_black_large.gif
hotrodbike_blue_large.gif
hotrodbike_f_large.gif
hotrodbike_f_silver_large.gif
hotrodbike_large.gif
hotrodbike_red_large.gif
hotrodbike_silver_large.gif
hotrodbike_yellow_large.gif
innertube_large.gif
julianax_r_02_black_large.gif
julianax_r_02_blue_large.gif
julianax_r_02_f_black_large.gif
julianax_r_02_f_blue_large.gif
julianax_r_02_f_gold_large.gif
julianax_r_02_f_green_large.gif
julianax_r_02_f_red_large.gif
julianax_r_02_gold_large.gif
julianax_r_02_green_large.gif
julianax_r_02_red_large.gif
julianax_r_02_silver_large.gif
julianax_r_02_yellow_large.gif
mb_shoes_large.gif
mb_tires_large.gif
no_image_available_large.gif
pedal_large.gif
racer02_black_f_large.gif
racer02_black_large.gif
racer02_blue_f_large.gif
racer02_blue_large.gif
racer02_green_f_large.gif
racer02_green_large.gif
racer02_red_large.gif
racer02_silver_large.gif
racer02_yellow_f_large.gif
racer_black_large.gif
racer_blue_large.gif
racer_red_large.gif
racer_silver_large.gif
racer_yellow_large.gif
roadster_black_large.gif
roadster_blue_large.gif
roadster_green_f_large.gif
roadster_green_large.gif
roadster_large.gif
roadster_purple_f_large.gif
roadster_purple_large.gif
roadster_red_f_large.gif
roadster_red_large.gif
roadster_silver_large.gif
roadster_yellow_f_large.gif
roadster_yellow_large.gif
saddle_large.gif
shorts_female_large.gif
shorts_male_large.gif
silver_chain_large.gif
silver_pedal_large.gif
silver_sprocket_large.gif
single_headlight_large.gif
sprocket_large.gif
street_tires_large.gif
superlight_black_f_large.gif
superlight_black_large.gif
superlight_blue_f_large.gif
superlight_blue_large.gif
superlight_metalicgreen_f_large.gif
superlight_metalicgreen_large.gif
superlight_red_f_large.gif
superlight_red_large.gif
superlight_silver_f_large.gif
superlight_silver_large.gif
superlight_yellow_f_large.gif
superlight_yellow_large.gif
tail_lights_large.gif
thumbnails
awc_jersey_female_small.gif
awc_jersey_male_small.gif
awc_tee_female_small.gif
awc_tee_male_small.gif
awc_tee_male_yellow_small.gif
bikepump_small.gif
bike_lock_small.gif
bike_shoes_small.gif
bike_shorts_female_small.gif
bike_shorts_male_small.gif
chain_lube_small.gif
chain_small.gif
clipless_pedals_small.gif
co2_4tire_small.gif
double_headlight_small.gif
fork_small.gif
frame_black_small.gif
frame_blue_small.gif
frame_red_small.gif
frame_silver_small.gif
frame_small.gif
frame_yellow_small.gif
handlebar_small.gif
handpump_small.gif
hotrodbike_black_small.gif
hotrodbike_blue_small.gif
hotrodbike_f_silver_small.gif
hotrodbike_f_small.gif
hotrodbike_red_small.gif
hotrodbike_silver_small.gif
hotrodbike_small.gif
hotrodbike_yellow_small.gif
innertube_small.gif
julianax_r_02_black_small.gif
julianax_r_02_blue_small.gif
julianax_r_02_f_black_small.gif
julianax_r_02_f_blue_small.gif
julianax_r_02_f_gold_small.gif
julianax_r_02_f_green_small.gif
julianax_r_02_f_red_small.gif
julianax_r_02_gold_small.gif
julianax_r_02_green_small.gif
julianax_r_02_red_small.gif
julianax_r_02_silver_small.gif
julianax_r_02_yellow_small.gif
mb_shoes_small.gif
mb_tires_small.gif
no_image_available_small.gif
pedal_small.gif
racer02_black_f_small.gif
racer02_black_small.gif
racer02_blue_f_small.gif
racer02_blue_small.gif
racer02_green_f_small.gif
racer02_green_small.gif
racer02_red_small.gif
racer02_silver_small.gif
racer02_yellow_f_small.gif
racer_black_small.gif
racer_blue_small.gif
racer_red_small.gif
racer_silver_small.gif
racer_yellow_small.gif
roadster_black_small.gif
roadster_blue_small.gif
roadster_green_f_small.gif
roadster_green_small.gif
roadster_purple_f_small.gif
roadster_purple_small.gif
roadster_red_f_small.gif
roadster_red_small.gif
roadster_silver_small.gif
roadster_small.gif
roadster_yellow_f_small.gif
roadster_yellow_small.gif
saddle_small.gif
shorts_female_small.gif
shorts_male_small.gif
silver_chain_small.gif
silver_pedal_small.gif
silver_sprocket_small.gif
single_headlight_small.gif
sprocket_small.gif
street_tires_small.gif
superlight_black_f_small.gif
superlight_black_small.gif
superlight_blue_f_small.gif
superlight_blue_small.gif
superlight_metalicgreen_f_small.gif
superlight_metalicgreen_small.gif
superlight_red_f_small.gif
superlight_red_small.gif
superlight_silver_f_small.gif
superlight_silver_small.gif
superlight_yellow_f_small.gif
superlight_yellow_small.gif
tail_lights_small.gif
tirepatch_kit_small.gif
water_bottle_cage_small.gif
water_bottle_small.gif
wheel_small.gif
tirepatch_kit_large.gif
water_bottle_cage_large.gif
water_bottle_large.gif
wheel_large.gif
vb
App_Code
ProductsView.vb
ShowProducts.aspx
 

UpdatePanelTutorialCustom Sample

cs/App_Code/ProductsView.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Drawing;

namespace UpdatePanelTutorialCustom.CS
{
    public class ProductsView : CompositeControl
    {
        private int _pageSize;
        private ArrayList _cart;
        private static readonly object EventRowCommand = new object();

        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }

        public ArrayList Cart
        {
            get { return _cart; }
        }

        protected override void CreateChildControls() {
            base.CreateChildControls();

            Control parent;
            Control container;

            // Get a reference to the ScriptManager object for the page
            // if one exists.
            ScriptManager sm = ScriptManager.GetCurrent(Page);

            if (sm == null || !sm.EnablePartialRendering)
            {
                // If partial rendering is not enabled, set the parent
                // and container as a basic control. 
                container = new Control();
                parent = container;
            }
            else
            {
                // If partial rendering is enabled, set the parent as
                // a new UpdatePanel object and the container to the 
                // content template of the UpdatePanel object.
                UpdatePanel up = new UpdatePanel();
                container = up.ContentTemplateContainer;
                parent = up;
            }

            AddDataboundControls(container);

            Controls.Add(parent);
        }

        private void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string productID;
            
            if (e.CommandName == "AddToCart")
            {
                productID = ((GridView)sender).DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
                if (_cart == null) { GetCart(); }
                if (_cart.IndexOf(productID) == -1)
                    _cart.Add(productID);
                ViewState["Cart"] = _cart;
            }

            if (e.CommandName == "RemoveFromCart")
            {
                productID = ((GridView)sender).DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
                if (_cart == null) { GetCart(); }
                _cart.Remove(productID);
                ViewState["Cart"] = _cart;
            }

            this.OnRowCommand(new EventArgs());
        }

        private void GetCart()
        {
            if (ViewState["Cart"] == null)
                _cart = new ArrayList();
            else
                _cart = (ArrayList)ViewState["Cart"];
        }

        public event EventHandler RowCommand
        {
            add { Events.AddHandler(EventRowCommand, value); }
            remove { Events.RemoveHandler(EventRowCommand, value); }
        }

        protected virtual void OnRowCommand(EventArgs e)
        {
            EventHandler handler = (EventHandler)Events[EventRowCommand];
            if (handler != null)
            {
                handler(this, e);
            }
        }

        private void AddDataboundControls(Control parent)
        {
            SqlDataSource ds = new SqlDataSource();
            ds.ID = "ProductsSqlDataSource";
            ds.ConnectionString = 
              ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"].ConnectionString;
            ds.SelectCommand =
              "SELECT Production.ProductDescription.Description, Production.Product.Name, Production.ProductPhoto.ThumbnailPhotoFileName, " +
              "Production.Product.ProductID " +
              "FROM Production.Product INNER JOIN " +
              "Production.ProductProductPhoto ON Production.Product.ProductID = Production.ProductProductPhoto.ProductID INNER JOIN " +
              "Production.ProductPhoto ON Production.ProductProductPhoto.ProductPhotoID = Production.ProductPhoto.ProductPhotoID INNER JOIN " +
              "Production.ProductModelProductDescriptionCulture ON  " +
              "Production.Product.ProductModelID = Production.ProductModelProductDescriptionCulture.ProductModelID INNER JOIN " +
              "Production.ProductDescription ON  " +
              "Production.ProductModelProductDescriptionCulture.ProductDescriptionID = Production.ProductDescription.ProductDescriptionID";

            GridView gv = new GridView();
            gv.ID = "ProductsGridView";
            gv.DataSourceID = ds.ID;
            gv.AutoGenerateColumns = false;
            gv.DataKeyNames = new string[] { "ProductID" };
            gv.GridLines = GridLines.None;
            gv.HeaderStyle.BackColor = Color.LightGray;
            gv.AlternatingRowStyle.BackColor = Color.LightBlue;
            gv.AllowPaging = true;
            gv.PageSize = _pageSize;
            gv.RowCommand += this.GridView_RowCommand;
           
            ImageField if1 = new ImageField();
            if1.HeaderText = "Product";
            if1.DataImageUrlField = "ThumbnailPhotoFileName";
            if1.DataImageUrlFormatString = @"..\images\thumbnails\{0}";

            BoundField bf1 = new BoundField();
            bf1.HeaderText = "Name";
            bf1.DataField = "Name";

            BoundField bf2 = new BoundField();
            bf2.HeaderText = "Description";
            bf2.DataField = "Description";

            ButtonField btf1 = new ButtonField();
            btf1.Text = "Add";
            btf1.CommandName = "AddToCart";

            ButtonField btf2 = new ButtonField();
            btf2.Text = "Remove";
            btf2.CommandName = "RemoveFromCart";

            gv.Columns.Add(if1);
            gv.Columns.Add(bf1);
            gv.Columns.Add(bf2);
            gv.Columns.Add(btf1);
            gv.Columns.Add(btf2);

            parent.Controls.Add(new LiteralControl("<br />"));
            parent.Controls.Add(gv);
            parent.Controls.Add(new LiteralControl("<br />"));
            parent.Controls.Add(ds);
        }
    }
}