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

<script runat="server">

    protected void grid_RowCreated(Object sender, GridViewRowEventArgs e) {
        if(e.Row.RowType == DataControlRowType.Footer) {
            LiteralControl ltr = new LiteralControl("総ページ数：" + grid.PageCount);
            e.Row.Cells[e.Row.Cells.Count - 1].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[e.Row.Cells.Count - 1].Wrap = false;
            e.Row.Cells[e.Row.Cells.Count - 1].Controls.Add(ltr);
        }
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="cph" Runat="Server">
    <asp:GridView ID="grid" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
        CellPadding="4" DataKeyNames="isbn" DataSourceID="sds" ForeColor="#333333" GridLines="None"
        PageSize="5" ShowFooter="True" OnRowCreated="grid_RowCreated">
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <Columns>
            <asp:HyperLinkField DataNavigateUrlFields="isbn" DataNavigateUrlFormatString="http://www.wings.msn.to/index.php/-/A-03/{0}/"
                DataTextField="isbn" DataTextFormatString="{0}" HeaderText="ISBNコード" />
            <asp:BoundField DataField="title" HeaderText="書名" SortExpression="title" />
            <asp:BoundField DataField="price" DataFormatString="{0:#,###円}" HeaderText="価格" HtmlEncode="False"
                SortExpression="price" />
            <asp:BoundField DataField="published" DataFormatString="{0:yyyy年MM月dd日}" HeaderText="発売日"
                HtmlEncode="False" SortExpression="published" />
            <asp:CheckBoxField DataField="cdrom" HeaderText="CD-ROM" SortExpression="cdrom" />
            <asp:ImageField DataAlternateTextField="title" DataImageUrlField="isbn" DataImageUrlFormatString="http://www.wings.msn.to/books/{0}/{0}.jpg"
                HeaderText="表紙" ReadOnly="True">
                <ControlStyle Height="48px" Width="35px" />
            </asp:ImageField>
            <asp:CommandField ButtonType="Button" ShowDeleteButton="True" ShowEditButton="True" />
        </Columns>
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    <asp:SqlDataSource ID="sds" runat="server" ConnectionString="<%$ ConnectionStrings:MyDB %>"
        DeleteCommand="DELETE FROM [Book] WHERE [isbn] = @original_isbn"
        InsertCommand="INSERT INTO [Book] ([isbn], [title], [price], [published], [cdrom]) VALUES (@isbn, @title, @price, @published, @cdrom)"
        OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [isbn], [title], [price], [published], [cdrom] FROM [Book]"
        UpdateCommand="UPDATE [Book] SET [title] = @title, [price] = @price, [published] = @published, [cdrom] = @cdrom WHERE [isbn] = @original_isbn">
        <DeleteParameters>
            <asp:Parameter Name="original_isbn" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="title" Type="String" />
            <asp:Parameter Name="price" Type="Int32" />
            <asp:Parameter Name="published" Type="DateTime" />
            <asp:Parameter Name="cdrom" Type="Boolean" />
            <asp:Parameter Name="original_isbn" Type="String" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="isbn" Type="String" />
            <asp:Parameter Name="title" Type="String" />
            <asp:Parameter Name="price" Type="Int32" />
            <asp:Parameter Name="published" Type="DateTime" />
            <asp:Parameter Name="cdrom" Type="Boolean" />
        </InsertParameters>
    </asp:SqlDataSource>
</asp:Content>

