<%@ Page Language="VB" MasterPageFile="~/day02/MyMaster.master" Title="FormViewコントロール" %>

<script runat="server">

    Protected Sub fv_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs)
        If Not Page.IsValid Then
            e.Cancel = True
        End If
    End Sub

    Protected Sub fv_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs)
        If Not Page.IsValid Then
            e.Cancel = True
        End If
    End Sub

    Protected Sub fv_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs)
        Response.Redirect("Calendar.aspx")
    End Sub

    Protected Sub fv_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs)
        Response.Redirect("Calendar.aspx")
    End Sub

    Protected Sub fv_ItemDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewDeletedEventArgs)
        Response.Redirect("Calendar.aspx")
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="cph" Runat="Server">
    <br />
    <asp:FormView ID="fv" runat="server" CellPadding="4" DataKeyNames="sid" DataSourceID="sds"
        EmptyDataText="スケジュール情報がありません。" ForeColor="#333333" HeaderText="スケジュール詳細" OnItemDeleted="fv_ItemDeleted" OnItemInserted="fv_ItemInserted" OnItemInserting="fv_ItemInserting" OnItemUpdated="fv_ItemUpdated" OnItemUpdating="fv_ItemUpdating">
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <EditItemTemplate>
            <asp:Label ID="lblSid" runat="server" Text='<%# Bind("sid") %>' Visible="False"></asp:Label><br />
            件名:
            <asp:TextBox ID="txtSubject" runat="server" Text='<%# Bind("subject") %>'></asp:TextBox>
            <asp:RequiredFieldValidator ID="reqSubject" runat="server" ControlToValidate="txtSubject"
                Display="Dynamic" ErrorMessage="件名は必須です。" SetFocusOnError="True">*</asp:RequiredFieldValidator><br />
            日付:
            <asp:TextBox ID="txtSdate" runat="server" Text='<%# Bind("sdate", "{0:yyyy/MM/dd}") %>'></asp:TextBox>
            <asp:RequiredFieldValidator ID="reqSdate" runat="server" ControlToValidate="txtSdate"
                Display="Dynamic" ErrorMessage="日付は必須です。" SetFocusOnError="True">*</asp:RequiredFieldValidator>
            <asp:CompareValidator ID="cmpSdate" runat="server" ControlToValidate="txtSdate" ErrorMessage="日付はYYYY/MM/DD形式で入力してください。"
                Operator="DataTypeCheck" SetFocusOnError="True" Type="Date">*</asp:CompareValidator><br />
            時刻:
            <asp:TextBox ID="txtStime" runat="server" Columns="10" Text='<%# Bind("stime") %>'></asp:TextBox>
            <asp:RegularExpressionValidator ID="regStime" runat="server" ControlToValidate="txtStime"
                Display="Dynamic" ErrorMessage="時刻はHH:MM形式で入力してください。" SetFocusOnError="True"
                ValidationExpression="[0-9]{2}\:[0-9]{2}">*</asp:RegularExpressionValidator><br />
            メモ :
            <asp:TextBox ID="txtMemo" runat="server" Columns="25" Text='<%# Bind("memo") %>'></asp:TextBox><br />
            <br />
            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                Text="更新"></asp:LinkButton>
            <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                Text="キャンセル"></asp:LinkButton>
        </EditItemTemplate>
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <InsertItemTemplate>
            件名:
            <asp:TextBox ID="txtSubject" runat="server" Text='<%# Bind("subject") %>'></asp:TextBox>
            <asp:RequiredFieldValidator ID="reqSubject" runat="server" ControlToValidate="txtSubject"
                Display="Dynamic" ErrorMessage="件名は必須です。" SetFocusOnError="True">*</asp:RequiredFieldValidator><br />
            日付:
            <asp:TextBox ID="txtSdate" runat="server" Text='<%# Bind("sdate", "{0:yyyy年MM月dd日}") %>'></asp:TextBox>
            <asp:RequiredFieldValidator ID="reqSdate" runat="server" ControlToValidate="txtSdate"
                Display="Dynamic" ErrorMessage="日付は必須です。" SetFocusOnError="True">*</asp:RequiredFieldValidator>
            <asp:CompareValidator ID="cmpSdate" runat="server" ControlToValidate="txtSdate" ErrorMessage="日付はYYYY/MM/DD形式で入力してください。"
                Operator="DataTypeCheck" SetFocusOnError="True" Type="Date">*</asp:CompareValidator><br />
            時刻:
            <asp:TextBox ID="txtStime" runat="server" Columns="10" Text='<%# Bind("stime") %>'></asp:TextBox>
            <asp:RegularExpressionValidator ID="regStime" runat="server" ControlToValidate="txtStime"
                Display="Dynamic" ErrorMessage="時刻はHH:MM形式で入力してください。" SetFocusOnError="True"
                ValidationExpression="[0-9]{2}\:[0-9]{2}">*</asp:RegularExpressionValidator><br />
            メモ :
            <asp:TextBox ID="txtMemo" runat="server" Columns="25" Text='<%# Bind("memo") %>'></asp:TextBox><br />
            &nbsp;<br />
            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                Text="挿入"></asp:LinkButton>
            <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                Text="キャンセル"></asp:LinkButton>
        </InsertItemTemplate>
        <ItemTemplate>
            件名:
            <asp:Label ID="lblSubject" runat="server" Text='<%# Bind("subject") %>'></asp:Label><br />
            日付:
            <asp:Label ID="lblSdate" runat="server" Text='<%# Bind("sdate", "{0:yyyy年MM月dd日}") %>'></asp:Label><br />
            時刻:
            <asp:Label ID="lblStime" runat="server" Text='<%# Bind("stime") %>'></asp:Label><br />
            メモ :
            <asp:Label ID="lblMemo" runat="server" Text='<%# Bind("memo") %>'></asp:Label><br />
            <br />
            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                Text="編集"></asp:LinkButton>
            <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                Text="削除"></asp:LinkButton>
            <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                Text="新規作成"></asp:LinkButton>
        </ItemTemplate>
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    </asp:FormView>
    <br />
    <asp:SqlDataSource ID="sds" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:MyDB %>"
        DeleteCommand="DELETE FROM [Schedule] WHERE [sid] = @original_sid AND [subject] = @original_subject AND [sdate] = @original_sdate AND [stime] = @original_stime AND [memo] = @original_memo"
        InsertCommand="INSERT INTO [Schedule] ([subject], [sdate], [stime], [memo]) VALUES (@subject, @sdate, @stime, @memo)"
        OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [sid], [subject], [sdate], [stime], [memo] FROM [Schedule] WHERE ([sid] = @sid)"
        UpdateCommand="UPDATE [Schedule] SET [subject] = @subject, [sdate] = @sdate, [stime] = @stime, [memo] = @memo WHERE [sid] = @original_sid AND [subject] = @original_subject AND [sdate] = @original_sdate AND [stime] = @original_stime AND [memo] = @original_memo">
        <DeleteParameters>
            <asp:Parameter Name="original_sid" Type="Int32" />
            <asp:Parameter Name="original_subject" Type="String" />
            <asp:Parameter Name="original_sdate" Type="DateTime" />
            <asp:Parameter Name="original_stime" Type="String" />
            <asp:Parameter Name="original_memo" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="subject" Type="String" />
            <asp:Parameter Name="sdate" Type="DateTime" />
            <asp:Parameter Name="stime" Type="String" />
            <asp:Parameter Name="memo" Type="String" />
            <asp:Parameter Name="original_sid" Type="Int32" />
            <asp:Parameter Name="original_subject" Type="String" />
            <asp:Parameter Name="original_sdate" Type="DateTime" />
            <asp:Parameter Name="original_stime" Type="String" />
            <asp:Parameter Name="original_memo" Type="String" />
        </UpdateParameters>
        <SelectParameters>
            <asp:QueryStringParameter Name="sid" QueryStringField="sid" Type="Int32" />
        </SelectParameters>
        <InsertParameters>
            <asp:Parameter Name="subject" Type="String" />
            <asp:Parameter Name="sdate" Type="DateTime" />
            <asp:Parameter Name="stime" Type="String" />
            <asp:Parameter Name="memo" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
</asp:Content>

