Solved

Adding values of a column when checkbox is clicked

Posted on 2013-01-13
2
321 Views
Last Modified: 2013-01-13
I have been at this for a week and still cant figure it  out. I have a listview with a checkbox column and another column with a price. When user checks a checkbox, i want to add the values and when they deselect...i want to decrement the value. I still cant get thos going...

If there are previous rows that user has checked and paid...i have those checkboxes disbaled so I dont add those rows

Each row is $50 (as an example)

Issue I have now is : I click a checkbox, code loops thru the rows and value is $50. This is correct. I check another checkbox...code loops and now the value is $150...this is because first check has the field as 50, then it loops and i have 2 checkboxes so that's 100 for total of 150...it should be 100 not 150

 <asp:CheckBox runat="server" AutoPostBack="true" OnCheckedChanged="onCheckChange_chkSelect"    ID="chkSelect" />

 if (!Page.IsPostBack)
        {
           x_amount.Text = "0";
       }

protected void onCheckChange_chkSelect(object sender, EventArgs e)
    {
              //add and decrement total
        foreach (var provider in lvPayment.Items)
        {
            CheckBox providerChecked = (CheckBox)provider.FindControl("chkSelect");
            Label price = (Label)provider.FindControl("lblPrice");
            Label paid = (Label)provider.FindControl("lblSubsDate");
           //paid.text means this user hasnt paid and based on that...the checkbox is enabled
          //if checkbox is disabled, dont add that value to the amount
            if (providerChecked.Checked && paid.Text == string.Empty && providerChecked.Enabled)
                x_amount.Text = (int.Parse(x_amount.Text) + int.Parse(price.Text)).ToString();
            else
                if (!providerChecked.Checked && paid.Text == string.Empty)
                   x_amount.Text = (int.Parse(x_amount.Text) - int.Parse(price.Text)).ToString();


        }

    }

Open in new window

0
Comment
Question by:Camillia
2 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 38772866
Maybe I'm missing something. What if you set x_amount.Text = "0"; on every postback then loop through, skip the test for "Enabled", skip the "else" and just add everything that is checked?
0
 
LVL 7

Author Comment

by:Camillia
ID: 38772948
you're totally correct. Let me try it.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now