Lets Have a fun with Technology.

BTemplates.com

Follow by Email

Sunday, September 14, 2008

Maintain Checkbox state In GridView While Paging in ASP.Net



There Are Most Cases when we required checkbox In grid view and Also Paging. But there are Problem Exists if We Used Paging And Checkbox Field When We Go Next Page Then Page Post back to Server and All Checked Value clear so There are Required to Maintain all checkbox
Value.
I take one Problem which exists with me when I am developing one job site during developing this site I come on point to maintain checked value. User search jobs there are lot’s of jobs So all jobs
Display in Grid view I have checkbox in Gridview.Now when user select multiple job and save I need to maintain all paging checked Data. I have Archived this one by following method.
First Of All when we Changed Page Or Press Submit button We Need to Stored All Checked Data. And Pressing Submit button Save Data From that Storage.
On Select Page Index Changing Event I Call Two Method.
After Gridview Bind called PupulateoldCheckValue Metohd
And On Page Index Changing called StoreOldValue Method.
private void StoreOldValue()
    {
        ArrayList categoryIDList = new ArrayList();
        foreach (GridViewRow row in grd_search_result.Rows)
        {
            Label can_id = (Label)row.FindControl("lbl_vacancy_code");
            bool result = ((CheckBox)row.FindControl("chk_select")).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 grd_search_result.Rows)
            {
                Label can_id = (Label)row.FindControl("lbl_vacancy_code");
                if (categoryIDList.Contains(can_id.Text))
                {
                    CheckBox myCheckBox = (CheckBox)row.FindControl("chk_select");
                    myCheckBox.Checked = true;
                }
            }
        }
    }




Prev Post                                                      All Post                                                           Next Post



3 comments:

  1. How to Fill DataGrid in ASP.Net 2.0. i use following Methods

    dim sql as string = "select Name,Dept from Master"
    dim ds as new dataset
    dim adp as new sqldataadapter(sql)
    adp.fill(ds,"xyz")
    datagrid1.datasource=ds.table("xyz").defaultview

    but when i click button fill data nothing is displayed so what should i do?

    ReplyDelete
  2. Hello There,

    You Are Right But You Need to Call Bind Methods Also.For Grid Type Control In Windows You Can Not Use DataBind Method But For Web Application you Need to call DataBind() Methods.

    So Just Include

    datagrid1.Databind(); at last.

    ReplyDelete