Solved

Adding values of a column when checkbox is clicked

Posted on 2013-01-13
2
322 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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