Solved

How do I keep a check box that is in a asp.net gridview checked when the page reloads

Posted on 2011-09-13
2
316 Views
Last Modified: 2013-12-17
I need for a checkbox in an ASP.NET gridview to stay checked after it has been checked so that when the page is brought up again with the gtidview that the checkbox that was checked earlier is still checked.  I get this error  Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

This is the code that I have. Written in C#
protected void Page_Load(object sender, EventArgs e)
        {
             _orderId = Request.QueryString["id"];
    
            //CHECK USER ACCESS
            UserID = HttpContext.Current.User.Identity.Name;
            util = new Utils(crmUrl, crmOrganization, crmUserName, crmPassword, crmDomain);
            ServiceSupportHasAccessRole = util.GetLoggedInUserAndCheckPermission(UserID, ServiceSupportAccessRole);

            //Test for ID if Null than print out error
            if (string.IsNullOrEmpty(_orderId))
            {
                gvParent.Visible = false;
                lblMessage.Text = "Unable to retrieve Order Id";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Black;

                if (!IsPostBack)
                {
                    // RETRIEVE PRODUCTS & BIND DATA TO THE GRID
                    BindParentGrid(new Guid(_orderId));

                    // Are there checked boxes?  
                    SaveCheckedValues(); //  <------------ Looks for the method
                }
            }
        }
 // This method is used to populate the saved checkbox values
        private void PopulateCheckedValues()
        {
            ArrayList orderDetails = (ArrayList)Session["CHECKED_ITEMS"];
            if (orderDetails != null && orderDetails.Count > 0)
            {
                foreach (GridViewRow gvrow in gvParent.Rows)
                {
                    int index = (int)gvParent.DataKeys[gvrow.RowIndex].Value;
                    if (orderDetails.Contains(index))
                    {
                        CheckBox chkRequestSNT = (CheckBox)gvrow.FindControl("chkRequestSNT");
                        CheckBox chkSNTOrder = (CheckBox)gvrow.FindControl("chkSNTOrder");
                        chkRequestSNT.Checked = true;
                        chkSNTOrder.Checked = true;

                    }
                }
            }
        }

        // This method is used to save the checked state of values   //DataKeyNames="Quantity"
        private void SaveCheckedValues()
        {
            ArrayList orderDetails = new ArrayList();
            int index = -1;
            foreach (GridViewRow gvrow in gvParent.Rows)
            {
                index = (int)gvParent.DataKeys[gvrow.RowIndex].Value; // <-----error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
                bool resultchkRequestSNT = ((CheckBox)gvrow.FindControl("chkRequestSNT")).Checked;
                bool resultchkSNTOrder = ((CheckBox)gvrow.FindControl("chkSNTOrder")).Checked;

                // Check in the Session
                if (Session["CHECKED_ITEMS"] != null)
                    orderDetails = (ArrayList)Session["CHECKED_ITEMS"];
                if (resultchkRequestSNT || resultchkSNTOrder)
                {
                    if (!orderDetails.Contains(index))
                        orderDetails.Add(index);
                }
                else
                    orderDetails.Remove(index);
            }
            if (orderDetails != null && orderDetails.Count > 0)
                Session["CHECKED_ITEMS"] = orderDetails;
        }

Open in new window

0
Comment
Question by:newjeep19
2 Comments
 

Author Comment

by:newjeep19
ID: 36530418
Please help....
0
 
LVL 16

Accepted Solution

by:
SriVaddadi earned 500 total points
ID: 36539288
Check the gvParent.DataKeys.Count first.

Also try
 index = (int)gvParent.DataKeys[gvrow.RowIndex-1].Value
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

685 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