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
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

691 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