我想在gridview的第1页上选中一个复选框,然后在gridview的第2页上选择另一个复选框 [英] I want to select a checkbox on page 1 of my gridview and select another one in page 2 of my gridview

查看:103
本文介绍了我想在gridview的第1页上选中一个复选框,然后在gridview的第2页上选择另一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想在我的gridview的第一页和整个gridview上选择一个复选框,而不取消选中在第一页上选择的复选框.下面是我使用的代码,但出现一个错误,提示错误:Sys.WebForms.PageRequestManagerServerErrorException:对象引用未设置为对象的实例."有人可以帮忙吗?

Hello I want to select a checkbox on page one of my gridview and page through my gridview without unchecking my checkbox that I selected on page one. Below is the code that I used but I am getting an error that says "Error: Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object." Can someone help please.

private void RememberOld()
{
    ArrayList candidateID = new ArrayList();
    int index = -1;
    foreach (GridViewRow row in GridView3.Rows)
    {
        index = int.Parse(GridView3.DataKeys[row.RowIndex].Value.ToString());
        bool result = ((CheckBox)row.FindControl("chkselect")).Checked;
        if (Session["CHECKED_ITEMS"] != null)
            candidateID = (ArrayList)Session["CHECKED_ITEMS"];
        if (result)
        {
            if (!candidateID.Contains(index))
                candidateID.Add(index);
        }
        else
            candidateID.Remove(index);

    }
    if (candidateID != null && candidateID.Count > 0)
        Session["CHECKED_ITEMS"] = candidateID;
}
private void RepopulateValues()
{
    ArrayList candidateID = (ArrayList)Session["CHECKED_ITEMS"];
    if (candidateID != null && candidateID.Count > 0)
    {
        foreach (GridViewRow row in GridView3.Rows)
        {
            int index = int.Parse(GridView3.DataKeys[row.RowIndex].Value.ToString());
            CheckBox mycheckbox = (CheckBox)row.FindControl("chkselect");
            if (candidateID.Contains(index))
            {
                mycheckbox.Checked = true;
            }

            else
                mycheckbox.Checked = false;
        }
    }
}


protected void GridView3_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    RememberOld();
    GridView3.PageIndex = e.NewPageIndex;
    GridView3.DataBind(); // Your logic to bind gridview
    RePopulateValues();
}

推荐答案

好吧,简而言之,您想在分页导航期间保持复选框的选择状态.
您正在使用哪个ASP.NET版本?如果是ASP.NET 4.0,则具有内置功能永久性GridView行选择 [维护GridView内部不同页面中选定复选框的状态 [
Well, in short you want to maintain the selection state of checkbox during Paging Navigation.
Which ASP.NET Version are you using ? If it is ASP.NET 4.0, it has a inbuilt features Persistent GridView Row Selection[^]

If it''s not, then you need to maintain the page state and you can take use of JavaScript.
Here is one same article at code Project
Maintaining States of Selected CheckBoxes in Different Pages inside the GridView[^]


这篇关于我想在gridview的第1页上选中一个复选框,然后在gridview的第2页上选择另一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆