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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

705 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

17 Experts available now in Live!

Get 1:1 Help Now