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

<script runat="server">

    protected void dv_ItemInserting(Object sender, DetailsViewInsertEventArgs e) {
        if(!Page.IsValid){
            e.Cancel = true;
        }
    }

    protected void dv_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e) {
        if(!Page.IsValid) {
            e.Cancel = true;
        }
    }

    protected void dv_ItemInserted(Object sender, DetailsViewInsertedEventArgs e) {
        Response.Redirect("Calendar.aspx");
    }

    protected void dv_ItemUpdated(Object sender, DetailsViewUpdatedEventArgs e) {
        Response.Redirect("Calendar.aspx");
    }

    protected void dv_ItemDeleted(Object sender, DetailsViewDeletedEventArgs e) {
        Response.Redirect("Calendar.aspx");
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="cph" Runat="Server">
    <asp:DetailsView ID="dv" runat="server" AutoGenerateRows="False" CellPadding="4"
        DataKeyNames="sid" DataSourceID="sds" EmptyDataText="スケジュール情報がありません。"
        ForeColor="#333333" GridLines="None" HeaderText="スケジュール詳細" Height="50px" OnItemInserting="dv_ItemInserting" OnItemUpdating="dv_ItemUpdating" OnItemInserted="dv_ItemInserted" OnItemUpdated="dv_ItemUpdated" OnItemDeleted="dv_ItemDeleted">
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <CommandRowStyle BackColor="#FFFFC0" Font-Bold="True" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <Fields>
            <asp:TemplateField HeaderText="件名" SortExpression="subject">
                <EditItemTemplate>
                    <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>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="txtSubject" runat="server" Text='<%# Bind("subject") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqSubject" runat="server" Display="Dynamic" ErrorMessage="件名は必須です。"
                        SetFocusOnError="True" ControlToValidate="txtSubject">*</asp:RequiredFieldValidator>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblSubject" runat="server" Text='<%# Bind("subject") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="日付" SortExpression="sdate">
                <EditItemTemplate>
                    <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>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="txtSdate" runat="server" Text='<%# Bind("sdate", "{0:yyyy年MM月dd日}") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqSdate" runat="server" ControlToValidate="txtSdate"
                        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>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblSdate" runat="server" Text='<%# Bind("sdate", "{0:yyyy年MM月dd日}") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="時刻" SortExpression="stime">
                <EditItemTemplate>
                    <asp:TextBox ID="txtStime" runat="server" Columns="10" Text='<%# Bind("stime") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqStime" runat="server" ControlToValidate="txtStime"
                        Display="Dynamic" ErrorMessage="時刻は必須です。" SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="regStime" runat="server" ControlToValidate="txtStime"
                        Display="Dynamic" ErrorMessage="時刻はHH:MM形式で入力してください。" SetFocusOnError="True"
                        ValidationExpression="[0-9]{2}\:[0-9]{2}">*</asp:RegularExpressionValidator>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="txtStime" runat="server" Columns="10" Text='<%# Bind("stime") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqStime" runat="server" ControlToValidate="txtStime"
                        Display="Dynamic" ErrorMessage="時刻は必須です。" SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="regStime" runat="server" ControlToValidate="txtStime"
                        Display="Dynamic" ErrorMessage="時刻はHH:MM形式で入力してください。" SetFocusOnError="True"
                        ValidationExpression="[0-9]{2}\:[0-9]{2}">*</asp:RegularExpressionValidator>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblStime" runat="server" Text='<%# Bind("stime") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="memo" HeaderText="メモ" SortExpression="memo" />
            <asp:CommandField ButtonType="Button" ShowDeleteButton="True" ShowEditButton="True"
                ShowInsertButton="True" />
        </Fields>
        <FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:DetailsView>
    <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:ValidationSummary ID="srtValid" runat="server" ShowMessageBox="True" ShowSummary="False" />
</asp:Content>

