您的位置:首页 > 编程语言 > ASP

asp.net 2.0常见问题技巧1

2013-07-01 17:24 309 查看
常见的一个应用场景,就是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");

            }

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: