ASP.NET从分页gridview获取已检查的行值

时间:2016-09-25 16:39:44

标签: c# asp.net gridview pagination

我创建了一个分页网格视图,其中包含多个选择的复选框。但是在网格视图中循环遍历数据行时,只有最后一页的行值才能得到。我知道这是因为分页

我的代码在

下面
       protected void btnSaveItemMapping_Click(object sender, EventArgs e)
          {

                grdCustomers.AllowPaging = false;

                    foreach (GridViewRow gvrow in grdCustomers.Rows)
                    {

                        CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkBoxBrandCustomers");
                        if (myCheckBox.Checked == true)
                        {

                             Label lblCustomerCode = (Label)gvrow.FindControl("lblCustomerCode");
                            //INSERT TO DATABSE
                         }
                    }
                   grdCustomers.AllowPaging = true
        }

在更改页面索引后保持选中复选框,如下面的代码。

 protected void grdCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        savechkdvls_cst();
        grdCustomers.PageIndex = e.NewPageIndex;
        if (Session["AllCustomers"] != null)
        {
            lstAllCustomers = (List<CustomerMaster>)Session["AllCustomers"];

        }
        grdCustomers.DataSource = lstAllCustomers;
        grdCustomers.DataBind();
        chkdvaluesp_cst();


    }
    private void chkdvaluesp_cst()
    {

        ArrayList usercontent = (ArrayList)Session["chkditems_customers"];

        if (usercontent != null && usercontent.Count > 0)
        {

            foreach (GridViewRow gvrow in grdCustomers.Rows)
            {

                int index = Convert.ToInt32(grdCustomers.DataKeys[gvrow.RowIndex].Value);

                if (usercontent.Contains(index))
                {

                    CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkBoxBrandCustomers");

                    myCheckBox.Checked = true;

                }

            }

        }

    }
    private void savechkdvls_cst()
    {

        ArrayList usercontent = new ArrayList();

        int index = -1;

        foreach (GridViewRow gvrow in grdCustomers.Rows)
        {

            index = Convert.ToInt32(grdCustomers.DataKeys[gvrow.RowIndex].Value);

            bool result = ((CheckBox)gvrow.FindControl("chkBoxBrandCustomers")).Checked;



            // Check in the Session

            if (Session["chkditems_customers"] != null)

                usercontent = (ArrayList)Session["chkditems_customers"];

            if (result)
            {

                if (!usercontent.Contains(index))

                    usercontent.Add(index);

            }

            else

                usercontent.Remove(index);

        }

        if (usercontent != null && usercontent.Count > 0)

            Session["chkditems_customers"] = usercontent;

    }

任何帮助将不胜感激。

0 个答案:

没有答案