Solved

Problem with 2 ListView Controls on .aspx

Posted on 2011-02-25
3
454 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
Comment Utility
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
Comment Utility
Make sure you have EnableViewState = "true" on the second ListView
0
 

Author Comment

by:jazzcatone
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Duplicate a row 2 29
Round a string to two digits 12 23
Change data in datatable 8 14
Rolling back a sequence of stored procedures 12 22
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

10 Experts available now in Live!

Get 1:1 Help Now