閲覧総計:11928  (本日:1  昨日:0)

 データベースにデータを新規登録した「作成日時」や、データの内容を編集更新した
「更新日時」を記録に残しておく事は、データ管理上とても有用である。
 MS AccessをDBとし、GridViewでデータの「作成日時」と「更新日時」を自動で登録
する方法を確認したので記録に残しておく。  09/12/01

【機能】GridViewの「編集」ボタンでAccessデータを編集する。
【稼働画面】

#ref(): File not found: "PageSize02.JPG" at page "データの作成日時、更新日時を自動登録する法"

Accessデータベース名:Access01.mdb
テーブル名:名簿

フィールド名データ型フィールドサイズ
ID (主キー)オートナンバー型長整数型
名前テキスト型50

※VWD2005、ASP.NET2.0ではAccess2007のDBファイル形式(*.accdb)は利用できない。
DBファイルは「Acces2002-2003形式(*.mdb)」で保存してASP.NETで利用する。

開発環境:VWD2005 + Access2003
サーバ:ASP.NET2.0 + Access2003

GridViewのページサイズを変更する(基本機能)

【方法】

#ref(): File not found: "PageSize01.JPG" at page "データの作成日時、更新日時を自動登録する法"

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    GridView1.PageSize = DropDownList1.SelectedValue
End Sub

【AutoDate01.aspx】

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
        If e.CommandName = "Insert" Then
            AccessDataSource1.InsertParameters.Clear()
            For Each key As String In Request.Form.AllKeys
                If key.Contains("$TextBox2") Then
                    AccessDataSource1.InsertParameters.Add(New ControlParameter("名前", TypeCode.String, key, "Text"))
                End If
            Next
            AccessDataSource1.Insert()
        End If

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>データの作成日時、更新日時を自動登録する</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        データの作成日時、更新日時を自動登録する 09/11/30<br />
        &nbsp;<br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
            DataSourceID="AccessDataSource1" EmptyDataText="表示するデータ レコードがありません。" ShowFooter="True" OnRowCommand="GridView1_RowCommand">
            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update"
                            Text="更新" />&nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False"
                                CommandName="Cancel" Text="キャンセル" />
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:Button ID="Button3" runat="server" CommandName="Insert" Text="挿入" />
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit"
                            Text="編集" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
                <asp:TemplateField HeaderText="名前" SortExpression="名前">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("名前") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Width="60px"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("名前") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="作成日時" DataFormatString="{0: yy/MM/dd HH:mm}" HeaderText="作成日時"
                    SortExpression="作成日時" />
                <asp:TemplateField HeaderText="更新日時" SortExpression="更新日時">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("更新日時", "{0: yy/MM/dd HH:mm}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="App_Data\MyDB.mdb"
            DeleteCommand="DELETE FROM `名簿2` WHERE `ID` = ?"
            InsertCommand="INSERT INTO `名簿2` (`名前`, `作成日時`, `更新日時`) VALUES (?, Now(), Now())"
            SelectCommand="SELECT `ID`, `名前`, `作成日時`, `更新日時` FROM `名簿2`"
            UpdateCommand="UPDATE `名簿2` SET `名前` = ?, `作成日時` = ?, `更新日時` = Now() WHERE `ID` = ?">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="名前" Type="String" />
                <asp:Parameter Name="作成日時" Type="DateTime" />
                <asp:Parameter Name="更新日時" Type="DateTime" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="名前" Type="String" />
                <asp:Parameter Name="作成日時" Type="DateTime" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
        </asp:AccessDataSource>
        <br />
    
    </div>
    </form>
</body>
</html>

【参考にしたページ】
  該当なし


選択肢 投票
参考になった 0  
ふつう 0  
参考にならなかった 0  

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS