<%@ Page Language="VB" EnableViewState="True" Trace="False" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

    Private mdtProducts As DataTable
    
    Sub Page_Load(sender As Object, e As EventArgs)
      If Not IsPostBack Then
        BindGrid()
      End If
    End Sub
    
    Sub BindGrid()
      mdtProducts = Cache("Products")
      If mdtProducts Is Nothing Then
        mdtProducts = CreateDataTable()
      End If
      DataGrid1.DataKeyField = "CustomerID"
      DataGrid1.DataSource = mdtProducts
      DataGrid1.DataBind()
      DisableViewState(DataGrid1)
    End Sub
    
    Sub DisableViewState(dg As DataGrid)
      For Each dgi As DataGridItem In dg.Items
        dgi.EnableViewState = False
      Next
    End Sub
    
    Function CreateDataTable() As DataTable
      Dim strSQL As String = "Select * From Customers"
      Dim strConnectionString As String = _
        String.Format("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source={0}", _
          Server.MapPath("webdb/Nwind.mdb"))
      Dim con As New OleDbConnection(strConnectionString)
      Dim da As New OleDbDataAdapter(strSQL, con)
      Dim dt As New DataTable()
    
      da.Fill(dt)
      Cache("Products") = dt
      Return(dt)
    End Function
    
    Sub DataGrid1_PageIndexChanged(sender As Object, e As DataGridPageChangedEventArgs)
      Trace.Warn("PageIndexChanged")
      DataGrid1.CurrentPageIndex = e.NewPageIndex
      BindGrid()
    End Sub
    
    Sub DataGrid1_EditCommand(sender As Object, e As DataGridCommandEventArgs)
      Trace.Warn("EditCommand")
      DataGrid1.EditItemIndex = e.Item.ItemIndex
      BindGrid()
    End Sub
    
    Sub DataGrid1_UpdateCommand(sender As Object, e As DataGridCommandEventArgs)
      Trace.Warn("UpdateCommand")
      Dim strCompanyName As String = CType(e.Item.Cells(1).Controls(0),TextBox).Text
      Dim intCustomerID As Integer = DataGrid1.DataKeys(e.Item.ItemIndex)
    
      UpdateCustomer(intCustomerID, strCompanyName)
      Cache.Remove("Products")
      DataGrid1.EditItemIndex = -1
      BindGrid()
    End Sub
    
    Sub DataGrid1_CancelCommand(sender As Object, e As DataGridCommandEventArgs)
      Trace.Warn("CancelCommand")
      DataGrid1.EditItemIndex = -1
      BindGrid()
    End Sub
    
    Sub UpdateCustomer(intCustomerID As Integer, strCompanyName As String)
      Dim strSQL As String = "Update Customers Set CompanyName=?" & _
        " Where CustomerID=?"
      Dim strConnectionString As String = _
        String.Format("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source={0}", _
          Server.MapPath("webdb/Nwind.mdb"))
      Dim con As New OleDbConnection(strConnectionString)
      Dim cmd As New OleDbCommand(strSQL, con)
    
      cmd.Parameters.Add("@1", strCompanyName)
      cmd.Parameters.Add("@2", intCustomerID)
      con.Open()
      Dim intAffectedRecords As Integer = cmd.ExecuteNonQuery()
      con.Close()
    End Sub

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <h3>ビューステート無効（その３）</h3>
        <asp:DataGrid id="DataGrid1" runat="server" OnCancelCommand="DataGrid1_CancelCommand" OnUpdateCommand="DataGrid1_UpdateCommand" OnEditCommand="DataGrid1_EditCommand" OnPageIndexChanged="DataGrid1_PageIndexChanged" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#3366CC">
            <FooterStyle forecolor="#003399" backcolor="#99CCCC"></FooterStyle>
            <SelectedItemStyle font-bold="True" forecolor="#CCFF99" backcolor="#009999"></SelectedItemStyle>
            <ItemStyle forecolor="#003399" backcolor="White"></ItemStyle>
            <HeaderStyle font-bold="True" horizontalalign="Center" forecolor="#CCCCFF" backcolor="#003399"></HeaderStyle>
            <Columns>
                <asp:BoundColumn DataField="CustomerID" ReadOnly="True" HeaderText="ID">
                    <HeaderStyle width="3em"></HeaderStyle>
                    <ItemStyle horizontalalign="Right"></ItemStyle>
                </asp:BoundColumn>
                <asp:BoundColumn DataField="CompanyName" HeaderText="得意先">
                    <HeaderStyle width="15em"></HeaderStyle>
                </asp:BoundColumn>
                <asp:EditCommandColumn ButtonType="PushButton" UpdateText="更新" CancelText="キャンセル" EditText="編集"></asp:EditCommandColumn>
            </Columns>
            <PagerStyle horizontalalign="Right" forecolor="#003399" position="Top" backcolor="#99CCCC" mode="NumericPages"></PagerStyle>
        </asp:DataGrid>
    </form>
</body>
</html>