0

I have checkbox in the gridview and a save button.....so far i have done to maintain the state of the checkbox in the pageindexchanging...in the save button click i want to save the values in which the checkbox is checked and save it in the database i need the code?

protected void ManageCalenderShift_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    StoreOldValue();
    EmployeeDetails.PageIndex = e.NewPageIndex;
    BindDataToGrid();
    PupulateoldCheckValue();
}
private void StoreOldValue()
{
    ArrayList categoryIDList = new ArrayList();
    foreach (GridViewRow row in EmployeeDetails.Rows)
    {
        Label can_id = (Label)row.FindControl("UserACENumber");
        bool result = ((CheckBox)row.FindControl("Chkgrid")).Checked;

        // Check in the Session
        if (Session["CHECKED_ITEMS"] != null)
            categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
        if (result)
        {
            if (!categoryIDList.Contains(can_id.Text))
                categoryIDList.Add(can_id.Text);
        }
        else
            categoryIDList.Remove(can_id.Text);
    }
    if (categoryIDList != null && categoryIDList.Count > 0)
        Session["CHECKED_ITEMS"] = categoryIDList;
}
private void PupulateoldCheckValue()
{
    ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
    if (categoryIDList != null && categoryIDList.Count > 0)
    {
        foreach (GridViewRow row in EmployeeDetails.Rows)
        {
            Label can_id = (Label)row.FindControl("UserACENumber");
            if (categoryIDList.Contains(can_id.Text))
            {
                CheckBox myCheckBox = (CheckBox)row.FindControl("Chkgrid");
                myCheckBox.Checked = true;
            }
        }
    }
}

in the save button click how to find the controls which is checked and save it in the database?

protected void Save_Click(object sender, EventArgs e)
{


foreach (GridViewRow row in EmployeeDetails.Rows)
    {
        CheckBox chkGrid = (CheckBox)EmployeeDetails.Rows[i].FindControl("chkGrid");
        if (chkGrid.Checked == true)
        {
            //There are two pages and four records in that i have checked the four records but controlis finding for the first two records in the first page if i use this code....
        }
    }



}

2 Answers 2

1

string strIDs;

string[] arrIDs;

strIDs = Request.Form["chkBox"].Replace("'", "");

arrIDs = strIDs.Split(',');

Using request.form["CheckBoxID"] you got value of checkbox in gridview.

Sign up to request clarification or add additional context in comments.

1 Comment

if you have more than one checkbox than take array.
0

simply search for checkbox control with ID, u can find it by using Page.fincontol(control ID), chk the checked values by foreach, open connection and save in db. dont forge to close connection

3 Comments

I have used foreach in that gridview...But i am facing a issue...for eg if there are two pages in which two records are thr with checked,in the page1 only finding the control.....
"codeproject.com/Answers/69485/…" chk this may b it can solve ur prob
try to cast vals in chk box and thn access thm using cheked property, this may help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.