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
314 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 use and create keystrokes in Netbeans IDE 8.0 for Windows.

776 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