locked
How To Show Footer Total Based On Conditon RRS feed

  • Question

  • User-807418713 posted

    Hello

    This is my Reapter Control Calcualtion it works perfect

     protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label l2 = e.Item.FindControl("LQ") as Label;
                
                pscTotal += Convert.ToDecimal(l2.Text);
            }
     if (e.Item.ItemType == ListItemType.Footer)
            {
                Label l4 = e.Item.FindControl("FLA") as Label;
                
                l4.Text = pscTotal.ToString();
             }
          }

    But i want footer total based on condition 

    Assume i have ine more Label it has data if any B data in that label it will not add in column total

    How to do so

    Wednesday, January 29, 2020 8:57 AM

Answers

  • User1535942433 posted

    Hi Gopi.MCA,

    Accroding to your description and codes,I suggest you could get the label value based on condition.Since I don't know what your condition and wheater all columns have the same condition,I create a little test for you.https://i.stack.imgur.com/V5Mvf.png

    More details,you could refer to below codes:

    ASPX:

    <asp:Repeater runat="server" ID="reperter" OnItemDataBound="reperter_ItemDataBound">
                    <HeaderTemplate>
                        <table>
                                <tr>
                                    <th></th>
                                    <th>No1</th>
                                    <th>No2</th>
                                    <th>No3</th>
                                    <th>No4</th>
                                    <th>No5</th>
                                    <th>No6</th>
                                </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td></td>
                            <td><asp:Label ID="lbNo1" runat="server" Text='<%#Eval("No1") %>'></asp:Label></td>
                            <td><asp:Label ID="lbNo2" runat="server" Text='<%#Eval("No2") %>'></asp:Label></td>
                            <td><asp:Label ID="lbNo3" runat="server" Text='<%#Eval("No3") %>'></asp:Label></td>
                            <td><asp:Label ID="lbNo4" runat="server" Text='<%#Eval("No4") %>'></asp:Label></td>
                            <td><asp:Label ID="lbNo5" runat="server" Text='<%#Eval("No5") %>'></asp:Label></td>
                            <td><asp:Label ID="lbNo6" runat="server" Text='<%#Eval("No6") %>'></asp:Label></td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        <tr>
                            <td>total</td>
                            <td><asp:Label ID="lbNo1Total" runat="server"></asp:Label></td>
                            <td><asp:Label ID="lbNo2Total" runat="server"></asp:Label></td>
                            <td><asp:Label ID="lbNo3Total" runat="server"></asp:Label></td>
                            <td><asp:Label ID="lbNo4Total" runat="server"></asp:Label></td>
                            <td><asp:Label ID="lbNo5Total" runat="server"></asp:Label></td>
                            <td><asp:Label ID="lbNo6Total" runat="server"></asp:Label></td>
                        </tr>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>

    Code-Behind:

    protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from ReapterTotal";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "ReapterTotal");
                reperter.DataSource = ds.Tables[0].DefaultView;
                reperter.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }
    
            protected int pscTotal1 = 0;
            protected int pscTotal2 = 0;
            protected int pscTotal3 = 0;
            protected int pscTotal4 = 0;
            protected int pscTotal5 = 0;
            protected int pscTotal6 = 0;
            protected void reperter_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    if (Convert.ToInt32(((Label)e.Item.FindControl("lbNo1")).Text) > 2)
                    {
                        pscTotal1 += Convert.ToInt32(((Label)e.Item.FindControl("lbNo1")).Text);
                    }
                    if (Convert.ToInt32(((Label)e.Item.FindControl("lbNo2")).Text) > 4)
                    {
                        pscTotal2 += Convert.ToInt32(((Label)e.Item.FindControl("lbNo2")).Text);
                    }
                    if (Convert.ToInt32(((Label)e.Item.FindControl("lbNo3")).Text) > 5)
                    {
                        pscTotal3 += Convert.ToInt32(((Label)e.Item.FindControl("lbNo3")).Text);
                    }
                    if (Convert.ToInt32(((Label)e.Item.FindControl("lbNo4")).Text) > 6)
                    {
                        pscTotal4 += Convert.ToInt32(((Label)e.Item.FindControl("lbNo4")).Text);
                    }
                    if (Convert.ToInt32(((Label)e.Item.FindControl("lbNo5")).Text) > 7)
                    {
                        pscTotal5 += Convert.ToInt32(((Label)e.Item.FindControl("lbNo5")).Text);
                    }
                    if (Convert.ToInt32(((Label)e.Item.FindControl("lbNo6")).Text) > 8)
                    {
                        pscTotal6 += Convert.ToInt32(((Label)e.Item.FindControl("lbNo6")).Text);
                    }
                }
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    Label lbNo1Total = (Label)e.Item.FindControl("lbNo1Total");
                    lbNo1Total.Text = pscTotal1.ToString();
                    Label lbNo2Total = (Label)e.Item.FindControl("lbNo2Total");
                    lbNo2Total.Text = pscTotal2.ToString();
                    Label lbNo3Total = (Label)e.Item.FindControl("lbNo3Total");
                    lbNo3Total.Text = pscTotal3.ToString();
                    Label lbNo4Total = (Label)e.Item.FindControl("lbNo4Total");
                    lbNo4Total.Text = pscTotal4.ToString();
                    Label lbNo5Total = (Label)e.Item.FindControl("lbNo5Total");
                    lbNo5Total.Text = pscTotal5.ToString();
                    Label lbNo6Total = (Label)e.Item.FindControl("lbNo6Total");
                    lbNo6Total.Text = pscTotal6.ToString();
                }
            }

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 30, 2020 5:00 AM

All replies

  • User-1716253493 posted

    Your requeirement is very clear. You only need if condition when adding total

    if(...){
        pscTotal += Convert.ToDecimal(l2.Text);
    }

    Thursday, January 30, 2020 2:27 AM
  • User1535942433 posted

    Hi Gopi.MCA,

    Accroding to your description and codes,I suggest you could get the label value based on condition.Since I don't know what your condition and wheater all columns have the same condition,I create a little test for you.https://i.stack.imgur.com/V5Mvf.png

    More details,you could refer to below codes:

    ASPX:

    <asp:Repeater runat="server" ID="reperter" OnItemDataBound="reperter_ItemDataBound">
                    <HeaderTemplate>
                        <table>
                                <tr>
                                    <th></th>
                                    <th>No1</th>
                                    <th>No2</th>
                                    <th>No3</th>
                                    <th>No4</th>
                                    <th>No5</th>
                                    <th>No6</th>
                                </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td></td>
                            <td><asp:Label ID="lbNo1" runat="server" Text='<%#Eval("No1") %>'></asp:Label></td>
                            <td><asp:Label ID="lbNo2" runat="server" Text='<%#Eval("No2") %>'></asp:Label></td>
                            <td><asp:Label ID="lbNo3" runat="server" Text='<%#Eval("No3") %>'></asp:Label></td>
                            <td><asp:Label ID="lbNo4" runat="server" Text='<%#Eval("No4") %>'></asp:Label></td>
                            <td><asp:Label ID="lbNo5" runat="server" Text='<%#Eval("No5") %>'></asp:Label></td>
                            <td><asp:Label ID="lbNo6" runat="server" Text='<%#Eval("No6") %>'></asp:Label></td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        <tr>
                            <td>total</td>
                            <td><asp:Label ID="lbNo1Total" runat="server"></asp:Label></td>
                            <td><asp:Label ID="lbNo2Total" runat="server"></asp:Label></td>
                            <td><asp:Label ID="lbNo3Total" runat="server"></asp:Label></td>
                            <td><asp:Label ID="lbNo4Total" runat="server"></asp:Label></td>
                            <td><asp:Label ID="lbNo5Total" runat="server"></asp:Label></td>
                            <td><asp:Label ID="lbNo6Total" runat="server"></asp:Label></td>
                        </tr>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>

    Code-Behind:

    protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from ReapterTotal";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "ReapterTotal");
                reperter.DataSource = ds.Tables[0].DefaultView;
                reperter.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }
    
            protected int pscTotal1 = 0;
            protected int pscTotal2 = 0;
            protected int pscTotal3 = 0;
            protected int pscTotal4 = 0;
            protected int pscTotal5 = 0;
            protected int pscTotal6 = 0;
            protected void reperter_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    if (Convert.ToInt32(((Label)e.Item.FindControl("lbNo1")).Text) > 2)
                    {
                        pscTotal1 += Convert.ToInt32(((Label)e.Item.FindControl("lbNo1")).Text);
                    }
                    if (Convert.ToInt32(((Label)e.Item.FindControl("lbNo2")).Text) > 4)
                    {
                        pscTotal2 += Convert.ToInt32(((Label)e.Item.FindControl("lbNo2")).Text);
                    }
                    if (Convert.ToInt32(((Label)e.Item.FindControl("lbNo3")).Text) > 5)
                    {
                        pscTotal3 += Convert.ToInt32(((Label)e.Item.FindControl("lbNo3")).Text);
                    }
                    if (Convert.ToInt32(((Label)e.Item.FindControl("lbNo4")).Text) > 6)
                    {
                        pscTotal4 += Convert.ToInt32(((Label)e.Item.FindControl("lbNo4")).Text);
                    }
                    if (Convert.ToInt32(((Label)e.Item.FindControl("lbNo5")).Text) > 7)
                    {
                        pscTotal5 += Convert.ToInt32(((Label)e.Item.FindControl("lbNo5")).Text);
                    }
                    if (Convert.ToInt32(((Label)e.Item.FindControl("lbNo6")).Text) > 8)
                    {
                        pscTotal6 += Convert.ToInt32(((Label)e.Item.FindControl("lbNo6")).Text);
                    }
                }
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    Label lbNo1Total = (Label)e.Item.FindControl("lbNo1Total");
                    lbNo1Total.Text = pscTotal1.ToString();
                    Label lbNo2Total = (Label)e.Item.FindControl("lbNo2Total");
                    lbNo2Total.Text = pscTotal2.ToString();
                    Label lbNo3Total = (Label)e.Item.FindControl("lbNo3Total");
                    lbNo3Total.Text = pscTotal3.ToString();
                    Label lbNo4Total = (Label)e.Item.FindControl("lbNo4Total");
                    lbNo4Total.Text = pscTotal4.ToString();
                    Label lbNo5Total = (Label)e.Item.FindControl("lbNo5Total");
                    lbNo5Total.Text = pscTotal5.ToString();
                    Label lbNo6Total = (Label)e.Item.FindControl("lbNo6Total");
                    lbNo6Total.Text = pscTotal6.ToString();
                }
            }

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 30, 2020 5:00 AM