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

<script runat="server">
    int sum  = 0;
    protected void grid_RowDataBound(Object sender, GridViewRowEventArgs e) {
        switch(e.Row.RowType) {
            case DataControlRowType.DataRow :
                Cart crt = (Cart)e.Row.DataItem;
                sum += crt.SubTotal;
                break;
            case DataControlRowType.Footer :
                String sumstr = sum.ToString("#,####円");
                e.Row.Cells[3].Controls.Add(new LiteralControl(sumstr));
                break;
        }
    }

    protected void Page_Load(Object sender, EventArgs e) {
        if(!Page.IsPostBack) {
            txtName.Text = Profile.CustomerName;
            txtAddress.Text = Profile.UserAddress;
        }
    }

    protected void btnOrder_Click(Object sender, EventArgs e) {
        Profile.CustomerName = txtName.Text;
        Profile.UserAddress = txtAddress.Text;
        Cart crt = new Cart();
        crt.InsertCartInfo();
        Session.Abandon();
        Response.Redirect("DataList.aspx");
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="cph" Runat="Server">
    <asp:GridView ID="grid" runat="server" Caption="カート精算" DataSourceID="ods"
        EmptyDataText="カートの中身は空です。" ShowFooter="True" OnRowDataBound="grid_RowDataBound" AutoGenerateColumns="False" DataKeyNames="isbn">
        <Columns>
            <asp:BoundField DataField="title" HeaderText="書名" />
            <asp:BoundField DataField="price" DataFormatString="{0:#,###円}" HeaderText="単価" HtmlEncode="False" />
            <asp:BoundField DataField="Quantity" HeaderText="購入数" />
            <asp:BoundField DataField="SubTotal" DataFormatString="{0:#,###円}" HeaderText="小計"
                HtmlEncode="False" />
            <asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
        </Columns>
    </asp:GridView>
    <asp:ObjectDataSource ID="ods" runat="server" DataObjectTypeName="Cart" DeleteMethod="DeleteCartInfo"
        SelectMethod="GetCartInfo" TypeName="Cart"></asp:ObjectDataSource>
    <br />
    お名前：<br />
    <asp:TextBox ID="txtName" runat="server" Columns="15"></asp:TextBox><br />
    お届け先（都道府県から）：<br />
    <asp:TextBox ID="txtAddress" runat="server" Columns="50"></asp:TextBox><br />
    <br />
    <asp:Button ID="btnOrder" runat="server" Text="注文を確定" OnClick="btnOrder_Click" />
</asp:Content>

