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

vb/App_Code/ProductsView.vb

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

Namespace UpdatePanelTutorialCustom.VB

    Public Class ProductsView
        Inherits CompositeControl

        Private _pageSize As Integer
        Private _cart As ArrayList
        Private Shared ReadOnly EventRowCommand As Object = New Object()

        Public Property PageSize() As Integer
            Get
                Return _pageSize
            End Get
            Set(ByVal value As Integer)
                _pageSize = value
            End Set
        End Property


        Public ReadOnly Property Cart() As ArrayList
            Get
                Return _cart
            End Get
        End Property

        Protected Overrides Sub CreateChildControls()
            MyBase.CreateChildControls()

            Dim parent As Control
            Dim container As Control

            ' Get a reference to the ScriptManager object for the page
            ' if one exists.
            Dim sm As ScriptManager = ScriptManager.GetCurrent(Page)

            If sm Is Nothing OrElse Not sm.EnablePartialRendering Then
                ' 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.
                Dim up As UpdatePanel = New UpdatePanel()
                container = up.ContentTemplateContainer
                parent = up
            End If

            AddDataboundControls(container)

            Controls.Add(parent)
        End Sub

        Private Sub GridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

            Dim productID As String

            If e.CommandName = "AddToCart" Then
                productID = CType(sender, GridView).DataKeys(Convert.ToInt32(e.CommandArgument)).Value.ToString()
                If _cart Is Nothing Then GetCart()
                If _cart.IndexOf(productID) = -1 Then _
                    _cart.Add(productID)
                ViewState("Cart") = _cart
            End If

            If e.CommandName = "RemoveFromCart" Then
                productID = CType(sender, GridView).DataKeys(Convert.ToInt32(e.CommandArgument)).Value.ToString()
                If _cart Is Nothing Then GetCart()
                _cart.Remove(productID)
                ViewState("Cart") = _cart
            End If

            Me.OnRowCommand(New EventArgs())
        End Sub

        Private Sub GetCart()
            If ViewState("Cart") Is Nothing Then
                _cart = New ArrayList()
            Else
                _cart = CType(ViewState("Cart"), ArrayList)
            End If
        End Sub

        Custom Event RowCommand As EventHandler
            RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
            End RaiseEvent
            AddHandler(ByVal value As EventHandler)
                Events.AddHandler(EventRowCommand, value)
            End AddHandler
            RemoveHandler(ByVal value As EventHandler)
                Events.RemoveHandler(EventRowCommand, value)
            End RemoveHandler
        End Event

        Protected Overridable Sub OnRowCommand(ByVal e As EventArgs)
            Dim handler As EventHandler = CType(Events(EventRowCommand), EventHandler)
            If handler IsNot Nothing Then
                handler(Me, e)
            End If
        End Sub

        Private Sub AddDataboundControls(ByVal parent As Control)
            Dim ds As SqlDataSource = 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"

            Dim gv As GridView = 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
            AddHandler gv.RowCommand, AddressOf Me.GridView_RowCommand

            Dim if1 As ImageField = New ImageField()
            if1.HeaderText = "Product"
            if1.DataImageUrlField = "ThumbnailPhotoFileName"
            if1.DataImageUrlFormatString = "..\images\thumbnails\{0}"

            Dim bf1 As BoundField = New BoundField()
            bf1.HeaderText = "Name"
            bf1.DataField = "Name"

            Dim bf2 As BoundField = New BoundField()
            bf2.HeaderText = "Description"
            bf2.DataField = "Description"

            Dim btf1 As ButtonField = New ButtonField()
            btf1.Text = "Add"
            btf1.CommandName = "AddToCart"

            Dim btf2 As ButtonField = 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)
        End Sub
    End Class
End Namespace