asp.net 2.0常見問題技巧1

來源:互聯網
上載者:User
   常見的一個應用情境,就是gridview中,當庫存量少於某個數時,背景顏色先變色
還有就是對某一列統計其總和,顯示在頁尾裡,下面分別闡述之
 首先是當庫存小於某個值時,行的背景顏色改變,比如

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // 確認"庫存量"欄位的值。
            //
            // 我們透過一個 DataBinder.Eval() 調用從將被綁定至 GridView 資料列的
            // 資料中取得"庫存量"欄位的值,傳遞給 DataBinder.Eval() 的第一個參
            // 數是將被綁定至 GridView 資料列的資料(也就是 e.Row.DataItem),
            // 傳遞給 DataBinder.Eval() 的第二個參數則是欄位名稱。
            decimal stock =
              Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "庫存量"));

            if (stock <= 0)
            {
                // 如果庫存量小於或等於 0,則將該資料列的背景色設定成紅色。
                e.Row.BackColor = Color.Red;
            }

            decimal totalMoney =
               Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "訂貨金額"));

            if (totalMoney > 0)
            {
                // 如果訂貨金額大於 0,則將該資料列的背景色設定成黃色。
                e.Row.BackColor = Color.Yellow;
            }

            // 累加訂貨金額並設定給變數 orderTotal。
            orderTotal += totalMoney;
        }
    }

而在頁面中加入footer模版
 <asp:TemplateField HeaderText="訂貨金額" SortExpression="訂貨金額">
                                            <EditItemTemplate>
                                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("訂貨金額", "{0:c}") %>'></asp:Label>
                                            </EditItemTemplate>
                                            <FooterTemplate>
                                                <asp:Label ID="OrderTotalLabel" runat="server" Font-Underline="True" ForeColor="Red"></asp:Label>
                                            </FooterTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("訂貨金額", "{0:c}") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>

// 建立一個變數來儲存訂貨金額加總。
    private decimal orderTotal = 0.0m;

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        // 提取當前的資料列。
        GridViewRow row = e.Row;

        // 如果正被建立的資料列是一個頁尾,則更新資料行加總。
        if (row.RowType == DataControlRowType.Footer)
        {
            // 取得頁尾當中的標籤控制項 OrderTotalTotal 。
            Label total = (Label)(e.Row.FindControl("OrderTotalLabel"));

            // 以貨幣格式來顯示訂貨金額加總。
            if (total != null)
            {
                total.Text = orderTotal.ToString("c");
            }
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.