Solved

Problem with 2 ListView Controls on .aspx

Posted on 2011-02-25
3
455 Views
Last Modified: 2012-06-27
OK this is driving me nuts. I have a .aspx page with 2 ListView Controls (ListViewA and ListViewB) on it. Both Listview controls have a Checkbox in it. If the user decides some action need to be taken on a record, they will check the checkbox for that particular record and then click a button at the bottom of the page.

The button click event itterates through the records in both ListViews and checks to see if the checkbox is checked. If so,
it will execute some code on that record. So I have two FOREACH statements to do this. 1 for each ListView

Problem: The test to see if the checkbox is checked on ListViewB always evaluates as FALSE. So no processing occurrs.

I stepped through the code and I can see that this is what is happening. Checkbox is checked...I swear. I have done all kinds of things to try to remedy this or figure out what the deal is. I tried switching the order of them in the .aspx markup, I tried changing the order of the ForEach statements, renaming he checkbox controls, and removing an AJAX Update panel that is on the .aspx page. None of this seems to matter. If anybody has any suggestions as to why this maybe happening it would be greatly appreciated. Code is below.

Jason
protected void AddRowstoInvoiceList()
        {

           //Loop Through ListView A This Works Fine !!
            foreach (ListViewDataItem lvi in BillListView.Items)
            {
                // Find the checkbox in each row
                CheckBox chkSelect = (CheckBox)lvi.FindControl("Chbxbill");

                // If the checkbox is checked then add the corresponding ID to our private InvoiceList


                // Get the CheckedID from the datakeynames property
                Int64 ID = Convert.ToInt64(BillListView.DataKeys[lvi.DisplayIndex].Value);
                //purge the list of any checked studies that are currently in the list, don't want studies to show up twice
                this.InvoiceList.RemoveAll(x => x.Studyid == ID);

     //Here is the test ..this one works as expected...Yay !!!!
                if (chkSelect.Checked == true)
                {
                    InvoicingRecord record = new InvoicingRecord();
                    // Add the ID to our list
                    record.Studyid = ID;

                    //Add Upload date
                    LinkButton ScoreDate = (LinkButton)lvi.FindControl("ScoreDateLBtn");
                    DateTime scrdt;
                    scrdt = Convert.ToDateTime(ScoreDate.Text);
                    record.ScoreDate = scrdt;

                    // Add patient
                    TextBox patlnk = (TextBox)lvi.FindControl("txbxPatient");
                    record.patient = patlnk.Text;

                    //Add item price 
                    TextBox pricebx = (TextBox)lvi.FindControl("txbxItemPrice");
                    decimal price = Convert.ToDecimal(pricebx.Text);
                    record.ItemPrice = price;

                    // Add the Notes

                    TextBox txtnote = (TextBox)lvi.FindControl("txbxNote");
                    record.note = txtnote.Text;

                    //Add the companyID

                    TextBox txttechid = (TextBox)lvi.FindControl("txbxTechId");
                    Guid g = new Guid(txttechid.Text);
                    record.techid_i = g;


                    //Add the InvoiceRecord to the list
                    this.InvoiceList.Add(record);

                }

            }


            // Loop Through TechTouchedListView The test for theckbox always evaluates to False...WHY ??
            foreach (ListViewDataItem rec in TechTouchedListView.Items)
            {
                // Find the checkbox in each row, it does infact find it when I step through the code
                CheckBox chkXXXX = (CheckBox)rec.FindControl("TechTouchedChbxbill");

                // If the checkbox is checked then add the corresponding ID to our private InvoiceList


                // Get the CheckedID from the datakeynames property
                Int64 ID = Convert.ToInt64(TechTouchedListView.DataKeys[rec.DisplayIndex].Value);
                //purge the list of any checked studies that are currently in the list, don't want studies to show up twice
                this.InvoiceList.RemoveAll(x => x.Studyid == ID);
//Here is the test ..this one always comes back as FALSE..I HAVE NO IDEA WHY.. What could cause this ??
                if (chkXXXX.Checked == true)
                {
                    InvoicingRecord record = new InvoicingRecord();
                    // Add the ID to our list
                    record.Studyid = ID;

                    //Add Upload date
                    LinkButton ScoreDate = (LinkButton)rec.FindControl("TechTouchedScoreDateLBtn");
                    DateTime scrdt;
                    scrdt = Convert.ToDateTime(ScoreDate.Text);
                    record.ScoreDate = scrdt;

                    // Add patient
                    TextBox patlnk = (TextBox)rec.FindControl("TechTouchedtxbxPatient");
                    record.patient = patlnk.Text;

                    //Add item price 
                    TextBox pricebx = (TextBox)rec.FindControl("TechTouchedtxbxItemPrice");
                    decimal price = Convert.ToDecimal(pricebx.Text);
                    record.ItemPrice = price;

                    // Add the Notes

                    TextBox txtnote = (TextBox)rec.FindControl("TechTouchedtxbxNote");
                    record.note = txtnote.Text;

                    //Add the companyID

                    TextBox txttechid = (TextBox)rec.FindControl("TechTouchedtxbxTechId");
                    Guid g = new Guid(txttechid.Text);
                    record.techid_i = g;


                    //Add the InvoiceRecord to the list
                    this.InvoiceList.Add(record);

                }

            }

     }

Open in new window

0
Comment
Question by:jazzcatone
3 Comments
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 34982248
More often than not, this is caused by setting the .Checked property of the checkboxes in the code-behind during a postback. What you want to do is make sure you check:

if(!Page.IsPostBack)
{
    PopulateMyControls();
}
0
 
LVL 6

Expert Comment

by:ksrsrinivasan
ID: 34985762
Make sure you have EnableViewState = "true" on the second ListView
0
 

Author Comment

by:jazzcatone
ID: 34991677
I had another control on the page. When the control reloaded (on control load) due to a postback (like strickdd said) oat was causing the second list view was reloading. This is essentially what was causing 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

Suggested Solutions

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…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
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…

929 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

19 Experts available now in Live!

Get 1:1 Help Now