六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 53|回复: 0

asp.net 2.0常见问题技巧1

[复制链接]

升级  73.25%

801

主题

801

主题

801

主题

探花

Rank: 6Rank: 6

积分
2465
 楼主| 发表于 2013-1-27 05:01:47 | 显示全部楼层 |阅读模式
常见的一个应用场景,就是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");
            }
        }
    }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表