Gridview 复选框在分页给出错误时保持状态

问题描述 投票:0回答:1

我的网格视图有问题

  1. 从一页移动到另一页时出现错误
  2. 它不保留分页上的复选框状态

KeepChecks 出现错误

错误信息 指数超出范围。必须为非负数且小于集合的大小。参数名称:索引 错误信息

下面是代码...

我们将感谢您的帮助和协助

 public partial class AdminConsole : System.Web.UI.Page
{

    SqlDataAdapter da;
    DataSet ds = new DataSet();


    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindData();
        }

    }





    private string GetConnectionString()
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

    }

    private void BindData()
    {
        SqlCommand cmd = new SqlCommand("SELECT UserID, EmployeeID, UserName, Dept FROM vw_UserID_Dept", con);

        try
        {
            da = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();
            da.Fill(ds);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            //GVRegisterNewCarton.DataSource = ds;
            //GVRegisterNewCarton.DataBind();

         if (!object.Equals(ds.Tables[0], null))
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                GVRegisterNewCarton.DataSource = ds.Tables[0];
                GVRegisterNewCarton.DataBind();
                Session["MyTable"] = ds.Tables[0];
            }
            else
            {
                GVRegisterNewCarton.DataSource = null;
                GVRegisterNewCarton.DataBind();
            }
        }
        else
        {
            GVRegisterNewCarton.DataSource = null;
            GVRegisterNewCarton.DataBind();
        }
    }
    catch (Exception ex)
    {

        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Error Binding Grid');", true);
        lblStatus.Text = ex.Message;

    }




    }

    protected void GVRegisterNewCarton_PageChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            KeepChecks();
            GVRegisterNewCarton.PageIndex = e.NewPageIndex;
            BindData();
            ApplyChecks();
        }

        catch (Exception ex)
        {

            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Error Binding Grid');", true);
            lblStatus.Text = ex.Message;

        }

    }




    private void KeepChecks()
{
    try
    {
        ArrayList chkList = new ArrayList();
        int index = -1;
        foreach (GridViewRow gvrow in GVRegisterNewCarton.Rows)
        {
            index = (int)GVRegisterNewCarton.DataKeys[gvrow.RowIndex].Value;
            bool result = ((CheckBox)gvrow.FindControl("chkSelectAdd")).Checked;

            if (Session["RemindChecks"] != null)
                chkList = (ArrayList)Session["RemindChecks"];
            if (result)
            {
                if (!chkList.Contains(index))
                    chkList.Add(index);
            }
            else
                chkList.Remove(index);
        }
        if (chkList != null && chkList.Count > 0)
            Session["RemindChecks"] = chkList;
    }
    catch (Exception ex)
    {

        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Error Binding Grid');", true);
        lblStatus.Text = ex.Message;

    }
}


private void ApplyChecks()
{
    try
    {
        ArrayList chkList = (ArrayList)Session["RemindChecks"];
        if (chkList != null && chkList.Count > 0)
        {
            foreach (GridViewRow gvrow in GVRegisterNewCarton.Rows)
            {
                int index = (int)GVRegisterNewCarton.DataKeys[gvrow.RowIndex].Value;
                if (chkList.Contains(index))
                {
                    CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkSelectAdd");
                    myCheckBox.Checked = true;
                }
            }
        }
    }

    catch (Exception ex)
    {

        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Error Binding Grid');", true);
        lblStatus.Text = ex.Message;

    }
}
c# checkbox gridview pagination
1个回答
0
投票

找到了解决该问题的更简单的替代解决方案

protected void GVRegisterNewCarton_PageChanging(object sender, GridViewPageEventArgs e)
{

    string pageId = string.Format("Page{0}", GVRegisterNewCarton.PageIndex);
    bool[] selectedCheckboxes = new bool[GVRegisterNewCarton.PageSize];
    for (int i = 0; i < GVRegisterNewCarton.Rows.Count; i++)
    {
        TableCell cell = GVRegisterNewCarton.Rows[i].Cells[0]; 
        selectedCheckboxes[i] = (cell.FindControl("chkSelectAdd") as CheckBox).Checked; 
    } 
    ViewState[pageId] = selectedCheckboxes;
    GVRegisterNewCarton.PageIndex = e.NewPageIndex;
    BindData();
    //Bind the gridview again 


}


protected void GVRegisterNewCarton_PreRender(object sender, EventArgs e) 

{ string pageId = string.Format("Page{0}",
    GVRegisterNewCarton.PageIndex); 
    bool[] selectedCheckboxes = ViewState[pageId] as bool[]; 
    if (selectedCheckboxes != null) { for (int i = 0;
        i < GVRegisterNewCarton.Rows.Count; i++) 
        {
            TableCell cell = GVRegisterNewCarton.Rows[i].Cells[0]; 
        (cell.FindControl("chkSelectAdd") as CheckBox).Checked = selectedCheckboxes[i]; 
    } 
    } 
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.