Click here to Skip to main content
16,004,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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();
    }
}
Posted
Updated 10-Mar-12 3:03am
v4
Comments
ProEnggSoft 10-Mar-12 9:03am    
Edit: Indentation of code blocks arranged - PES

1 solution

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:
XML
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"          OnRowCommand="GridView1_RowCommand"          OnRowEditing="GridView1_RowEditing"/>
}


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.

C#
protected void Page_Load(object sender, EventArgs e)
{
     BindGridView(); 
}
 
Share this answer
 
Comments
Anele Ngqandu 10-Mar-12 9:11am    
Hi Sir
But now why use OnRowCommand="GridView1_RowCommand" and OnRowEditing="GridView1_RowEditing" while my code is inside grvTravelClaims_RowDataBound event?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900