O'Reilly logo

Learning ASP.NET 2.0 with AJAX by Dan Hurwitz, Jesse Liberty, Brian MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Source Code Listings

The complete markup for the Default.aspx file in the AWProductData site is shown in Example 4-5, with the code-behind shown directly after in Example 4-6.

When you selected a product in the first grid, the order details appear below in the second grid.

Figure 4-26. When you selected a product in the first grid, the order details appear below in the second grid.

Example 4-5. Default.aspx for AWProductData

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb"
    Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        &nbsp;&nbsp;
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString=
                "<%$ ConnectionStrings:AdventureWorksConnectionString %>"
        SelectCommand="SELECT [ProductID], [Name], [ProductNumber], [MakeFlag],
             [SafetyStockLevel], [ReorderPoint] FROM [Production].[Product]"
        DeleteCommand="DELETE FROM [Production].[Product]
            WHERE [ProductID] = @original_ProductID AND [Name] = @original_Name
                AND [ProductNumber] = @original_ProductNumber
                AND [MakeFlag] = @original_MakeFlag
                AND [SafetyStockLevel] = @original_SafetyStockLevel
                AND [ReorderPoint] = @original_ReorderPoint"
        InsertCommand="INSERT INTO [Production].[Product] ([Name],
             [ProductNumber], [MakeFlag], [SafetyStockLevel], [ReorderPoint])
             VALUES (@Name, @ProductNumber, @MakeFlag, @SafetyStockLevel,
                @ReorderPoint)"
        UpdateCommand="UPDATE [Production].[Product]
                SET [Name] = @Name, [ProductNumber] = @ProductNumber,
                 [MakeFlag] = @MakeFlag,
                 [SafetyStockLevel] = @SafetyStockLevel,
                 [ReorderPoint] = @ReorderPoint
            WHERE [ProductID] = @original_ProductID
                AND [Name] = @original_Name
                AND [ProductNumber] = @original_ProductNumber
                AND [MakeFlag] = @original_MakeFlag
                AND [SafetyStockLevel] = @original_SafetyStockLevel
                AND [ReorderPoint] = @original_ReorderPoint"
        ConflictDetection="CompareAllValues"
        OldValuesParameterFormatString="original_{0}" >
        <DeleteParameters>
            <asp:Parameter Name="original_ProductID" Type="Int32" />
            <asp:Parameter Name="original_Name" Type="String" />
            <asp:Parameter Name="original_ProductNumber" Type="String" />
            <asp:Parameter Name="original_MakeFlag" Type="Boolean" />
            <asp:Parameter Name="original_SafetyStockLevel" Type="Int16" />
            <asp:Parameter Name="original_ReorderPoint" Type="Int16" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="ProductNumber" Type="String" />
            <asp:Parameter Name="MakeFlag" Type="Boolean" />
            <asp:Parameter Name="SafetyStockLevel" Type="Int16" />
            <asp:Parameter Name="ReorderPoint" Type="Int16" />
            <asp:Parameter Name="original_ProductID" Type="Int32" />
            <asp:Parameter Name="original_Name" Type="String" />
            <asp:Parameter Name="original_ProductNumber" Type="String" />
            <asp:Parameter Name="original_MakeFlag" Type="Boolean" />
            <asp:Parameter Name="original_SafetyStockLevel" Type="Int16" />
            <asp:Parameter Name="original_ReorderPoint" Type="Int16" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="ProductNumber" Type="String" />
            <asp:Parameter Name="MakeFlag" Type="Boolean" />
            <asp:Parameter Name="SafetyStockLevel" Type="Int16" />
            <asp:Parameter Name="ReorderPoint" Type="Int16" />
        </InsertParameters>
        </asp:SqlDataSource>
                <asp:GridView ID="GridView1" runat="server"
                    AllowPaging="True" AllowSorting="True"
                    AutoGenerateColumns="False"
                    DataKeyNames="ProductID" DataSourceID="SqlDataSource1"
                    BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
                    BorderWidth="1px" CellPadding="3" CellSpacing="2"
                    OnRowDataBound="GridView1_RowDataBound"
                    OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                    <Columns>
                        <asp:CommandField ButtonType="Button"
                                 ShowDeleteButton="True" ShowEditButton="True" />
                        <asp:BoundField DataField="ProductID" HeaderText="ID"
                                 InsertVisible="False"
                                 ReadOnly="True" SortExpression="ProductID" />
                        <asp:BoundField DataField="Name" HeaderText="Name"
                                 SortExpression="Name" />
                        <asp:BoundField DataField="ProductNumber"
                                 HeaderText="ProductNumber"
                                 SortExpression="ProductNumber" />
                        <asp:CheckBoxField DataField="MakeFlag"
                            HeaderText="MakeFlag" SortExpression="MakeFlag" />
                        <asp:BoundField DataField="SafetyStockLevel"
                            HeaderText="SafetyStockLevel"
                            SortExpression="SafetyStockLevel" />
                        <asp:BoundField DataField="ReorderPoint"
                            HeaderText="ReorderPoint"
                            SortExpression="ReorderPoint" />
                    </Columns>
                    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True"
                          ForeColor="White" />
                    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                    <HeaderStyle BackColor="#A55129" Font-Bold="True"
                          ForeColor="White" />
                </asp:GridView>
                <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>

Example 4-6. Default.aspx.vb for AWProductData

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, _
                   ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim cellProductNumber As TableCell = e.Row.Cells(3) ' ProductNumber
            If cellProductNumber.Text.Substring(0, 2) = "CA" Then
                cellProductNumber.ForeColor = Drawing.Color.Green
            End If

            Dim cellMakeFlag As TableCell = e.Row.Cells(4) ' MakeFlag column
            Dim cb As CheckBox = CType(cellMakeFlag.Controls(0), CheckBox)
            If cb.Checked Then
                e.Row.Cells(2).ForeColor = Drawing.Color.Red
            End If
        End If
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, _
            ByVal e As System.EventArgs)
        If GridView1.SelectedRow.RowType = DataControlRowType.DataRow Then
            Dim cellName As TableCell = GridView1.SelectedRow.Cells(2) ' Name
            txtName.Text = cellName.Text
        End If
    End Sub
End Class

The complete markup for the Default.aspx file in the AWProductDataOrderDetails site is shown in Example 4-7, and the code-behind is shown in Example 4-8.

Example 4-7. Default.aspx for AWProductDataOrderDetails

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb"
    Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        &nbsp;&nbsp;
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString=
                "<%$ ConnectionStrings:AdventureWorksConnectionString %>"
            SelectCommand="SELECT [ProductID], [Name], [ProductNumber], [MakeFlag],
                 [SafetyStockLevel], [ReorderPoint] FROM [Production].[Product]"
            DeleteCommand="DELETE FROM [Production].[Product]
               WHERE [ProductID] = @original_ProductID
                   AND [Name] = @original_Name
                   AND [ProductNumber] = @original_ProductNumber
                   AND [MakeFlag] = @original_MakeFlag
                   AND [SafetyStockLevel] = @original_SafetyStockLevel
                   AND [ReorderPoint] = @original_ReorderPoint"
           InsertCommand="INSERT INTO [Production].[Product] ([Name],
               [ProductNumber], [MakeFlag], [SafetyStockLevel], [ReorderPoint])
               VALUES (@Name, @ProductNumber, @MakeFlag, @SafetyStockLevel,
                   @ReorderPoint)"
           UpdateCommand="UPDATE [Production].[Product]
               SET [Name] = @Name, [ProductNumber] = @ProductNumber,
                    [MakeFlag] = @MakeFlag,
                    [SafetyStockLevel] = @SafetyStockLevel,
                    [ReorderPoint] = @ReorderPoint
               WHERE [ProductID] = @original_ProductID
                   AND [Name] = @original_Name
                   AND [ProductNumber] = @original_ProductNumber
                   AND [MakeFlag] = @original_MakeFlag
                   AND [SafetyStockLevel] = @original_SafetyStockLevel
                   AND [ReorderPoint] = @original_ReorderPoint"
               ConflictDetection="CompareAllValues"
               OldValuesParameterFormatString="original_{0}" >
           <DeleteParameters>
               <asp:Parameter Name="original_ProductID" Type="Int32" />
               <asp:Parameter Name="original_Name" Type="String" />
               <asp:Parameter Name="original_ProductNumber" Type="String" />
               <asp:Parameter Name="original_MakeFlag" Type="Boolean" />
               <asp:Parameter Name="original_SafetyStockLevel" Type="Int16" />
               <asp:Parameter Name="original_ReorderPoint" Type="Int16" />
           </DeleteParameters>
           <UpdateParameters>
               <asp:Parameter Name="Name" Type="String" />
               <asp:Parameter Name="ProductNumber" Type="String" />
               <asp:Parameter Name="MakeFlag" Type="Boolean" />
               <asp:Parameter Name="SafetyStockLevel" Type="Int16" />
               <asp:Parameter Name="ReorderPoint" Type="Int16" />
               <asp:Parameter Name="original_ProductID" Type="Int32" />
               <asp:Parameter Name="original_Name" Type="String" />
               <asp:Parameter Name="original_ProductNumber" Type="String" />
               <asp:Parameter Name="original_MakeFlag" Type="Boolean" />
               <asp:Parameter Name="original_SafetyStockLevel" Type="Int16" />
               <asp:Parameter Name="original_ReorderPoint" Type="Int16" />
           </UpdateParameters>
           <InsertParameters>
               <asp:Parameter Name="Name" Type="String" />
               <asp:Parameter Name="ProductNumber" Type="String" />
               <asp:Parameter Name="MakeFlag" Type="Boolean" />
               <asp:Parameter Name="SafetyStockLevel" Type="Int16" />
               <asp:Parameter Name="ReorderPoint" Type="Int16" />
           </InsertParameters>
       </asp:SqlDataSource>
               <asp:GridView ID="GridView1" runat="server"
                   AllowPaging="True" AllowSorting="True"
                   AutoGenerateColumns="False"
                   DataKeyNames="ProductID" DataSourceID="SqlDataSource1"
                   BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
                   BorderWidth="1px" CellPadding="3" CellSpacing="2"
                   OnRowDataBound="GridView1_RowDataBound"
                   OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                   <Columns>
                       <asp:CommandField ButtonType="Button"
                           ShowDeleteButton="True"
                           ShowEditButton="True"
                           ShowSelectButton="True" />
                       <asp:BoundField DataField="ProductID" HeaderText="ID"
                           InsertVisible="False"
                           ReadOnly="True"
                           SortExpression="ProductID" />
                       <asp:BoundField DataField="Name" HeaderText="Name"
                           SortExpression="Name" />
                      <asp:BoundField DataField="ProductNumber"
                          HeaderText="ProductNumber"
                          SortExpression="ProductNumber" />
                      <asp:CheckBoxField DataField="MakeFlag"
                          HeaderText="MakeFlag" SortExpression="MakeFlag" />
                      <asp:BoundField DataField="SafetyStockLevel"
                          HeaderText="SafetyStockLevel"
                          SortExpression="SafetyStockLevel" />
                      <asp:BoundField DataField="ReorderPoint"
                          HeaderText="ReorderPoint"
                          SortExpression="ReorderPoint" />
                  </Columns>
                  <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                  <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                  <SelectedRowStyle BackColor="#738A9C" Font-Bold="True"
                          ForeColor="White" />
                  <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                  <HeaderStyle BackColor="#A55129" Font-Bold="True"
                          ForeColor="White" />
              </asp:GridView>
              <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
              <asp:TextBox ID="txtName" runat="server" ReadOnly="True">
                      </asp:TextBox>
              <br />
              <asp:GridView ID="GridView2" runat="server"
                  DataSourceID="AdventureWorksOrderDetails"
                  BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
                          BorderWidth="1px"
                  CellPadding="3" CellSpacing="2">
                  <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                  <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                  <SelectedRowStyle BackColor="#738A9C" Font-Bold="True"
                          ForeColor="White" />
                  <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                  <HeaderStyle BackColor="#A55129" Font-Bold="True"
                          ForeColor="White" />
              </asp:GridView>
                <asp:SqlDataSource ID="AdventureWorksOrderDetails" runat="server"
                    ConnectionString=
                        "<%$ ConnectionStrings:AdventureWorksConnectionString %>"
                    SelectCommand="SELECT [SalesOrderID], [CarrierTrackingNumber],
                         [OrderQty], [UnitPrice], [UnitPriceDiscount], [LineTotal]
                         FROM [Sales].[SalesOrderDetail]
                         WHERE ([ProductID] = @ProductID)
                         ORDER BY [SalesOrderID]">
                    <SelectParameters>
                        <asp:ControlParameter
                            ControlID="GridView1"
                            Name="ProductID"
                            PropertyName="SelectedValue"
                            Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </ContentTemplate>
        </asp:UpdatePanel>

    </form>
</body>
</html>

Example 4-8. Default.aspx.vb for AWProductDataOrderDetails

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, _
                   ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim cellProductNumber As TableCell = e.Row.Cells(3) ' ProductNumber
            If cellProductNumber.Text.Substring(0, 2) = "CA" Then
                cellProductNumber.ForeColor = Drawing.Color.Green
            End If

            Dim cellMakeFlag As TableCell = e.Row.Cells(4) ' MakeFlag column
            Dim cb As CheckBox = CType(cellMakeFlag.Controls(0), CheckBox)
            If cb.Checked Then
                e.Row.Cells(2).ForeColor = Drawing.Color.Red
            End If
        End If
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, _
            ByVal e As System.EventArgs)
        If GridView1.SelectedRow.RowType = DataControlRowType.DataRow Then
            Dim cellName As TableCell = GridView1.SelectedRow.Cells(2) ' Name
            txtName.Text = cellName.Text
        End If
    End Sub
End Class

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required