Gridview页脚计算器 [英] Gridview Footer calculator

查看:63
本文介绍了Gridview页脚计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

试图创建一个页脚计算器.现在的问题是,跳过了if()statemets.这有什么想法吗?检查以下代码:

Hi guys

Trying to create a footer calculator. Problem is now the if() statemets are skiped. any ideas Y this hapens? Check the code below:

protected void grvTravelClaims_RowDataBound(object sender, GridViewRowEventArgs e)
{
    decimal _total = 0.0M;
    int totalItems = 0;
  
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lblKillometers = (Label)e.Row.FindControl("lblKillometers");
        decimal killometers = Decimal.Parse(lblKillometers.Text);
        _total += killometers;
        totalItems += 1;
       
    }
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        Label lblTotalKillometers = (Label)e.Row.FindControl("lblTotalKillometers");
        lblTotalKillometers.Text = _total.ToString();
    }
}

推荐答案

必须在每次回发中重新创建动态创建的控件.从您发布的代码看来,您可以改为在标记中执行此操作.如果不是,则必须在每次回发中重新创建动态控件.

您的标记代码应为:
Dynamically created controls must be re-created on every postback. From the code you posted it appears that you could do this in markup instead. If not, you''ll have to re-create the dynamic controls on every postback.

Your mark up code should be:
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"          OnRowCommand="GridView1_RowCommand"          OnRowEditing="GridView1_RowEditing"/>
}



关键是您需要将代码从Page_Load移到Page_Init.在Page_Load中添加的内容仅可用于一次回发的生命周期.



Key thing is that you need to move your code from Page_Load to Page_Init. Things added in the Page_Load last only for the lifecycle of one postback.

protected void Page_Load(object sender, EventArgs e)
{
     BindGridView(); 
}


这篇关于Gridview页脚计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆