Solved

Adding values of a column when checkbox is clicked

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 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