?
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
Medium Priority
?
321 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 2000 total points
ID: 36539288
Check the gvParent.DataKeys.Count first.

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.
Suggested Courses

770 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