C# 如何在启用了分页的gridview中维护控件的状态?

C# 如何在启用了分页的gridview中维护控件的状态?,c#,asp.net,state-management,C#,Asp.net,State Management,我有一个包含4列的gridview,其中包括一列。Approve有一个复选框,该复选框将在绑定后对数据集的所有行重复。我已将allowpaging=true&pageindexsize设置为10。现在,假设我选中第2行和第5行的复选框,并移动到第2页,然后返回到第1页,我在第1页选中的(第2行和第5行)复选框将重置为未选中状态。我知道原因,这是由于我们在OnPageIndexchange事件中对gridview的绑定。但是,即使我们从一个页面移动到另一个页面,是否有办法保持复选框的状态 谢谢您可

我有一个包含4列的gridview,其中包括一列。Approve有一个复选框,该复选框将在绑定后对数据集的所有行重复。我已将allowpaging=true&pageindexsize设置为10。现在,假设我选中第2行和第5行的复选框,并移动到第2页,然后返回到第1页,我在第1页选中的(第2行和第5行)复选框将重置为未选中状态。我知道原因,这是由于我们在OnPageIndexchange事件中对gridview的绑定。但是,即使我们从一个页面移动到另一个页面,是否有办法保持复选框的状态


谢谢

您可以使用会话来维护这些值。一页索引更改事件您需要向会话添加值并重新绑定复选框

这里有几个链接可以帮助你

  • 试试看,(您必须将两个事件复选框\u Checked和复选框\u PreRender设置为网格中的Checkbox控件。此外,还必须在网格中设置数据键,以便在Checks数组中有一个索引。)

    这些在静态GridViewUtils类中工作得最好

    public static GridViewRow GetRow(object sender)
    {
        Control c = (Control)sender;
        while ((null != c) && !(c is GridViewRow))
            c = c.NamingContainer;
        return (GridViewRow)c;
    }
    
    /// <summary>
    /// Get the Grid the row is in
    /// </summary>
    /// <param name="row"></param>
    /// <returns></returns>
    public static GridView GetGrid(this GridViewRow row)
    {
        Control c = (Control)row;
        while ((null != c) && !(c is GridView))
            c = c.NamingContainer;
        return (GridView)c;
    }
    
    /// <summary>
    /// Get the ID field value based on DataKey and row's index
    /// </summary>
    /// <param name="sender">Any web-control object in the grid view</param>
    /// <returns></returns>
    public static object GetRowDataKeyValue(object sender)
    {
        try
        {
            GridViewRow row = GetRow(sender);
            GridView grid = row.GetGrid();
            return grid.DataKeys[row.RowIndex].Value;
        }
        catch
        {
            return null;
        }
    }
    
    公共静态GridViewRow GetRow(对象发送方)
    {
    控制c=(控制)发送方;
    而((null!=c)&&&!(c是GridViewRow))
    c=c.命名容器;
    返回(GridViewRow)c;
    }
    /// 
    ///获取该行所在的网格
    /// 
    /// 
    /// 
    公共静态GridView GetGrid(此GridViewRow行)
    {
    控制c=(控制)行;
    而((null!=c)&&&!(c是GridView))
    c=c.命名容器;
    返回(GridView)c;
    }
    /// 
    ///基于DataKey和行的索引获取ID字段值
    /// 
    ///网格视图中的任何web控件对象
    /// 
    公共静态对象GetRowDataKeyValue(对象发送方)
    {
    尝试
    {
    GridViewRow行=GetRow(发送方);
    GridView grid=row.GetGrid();
    返回grid.DataKeys[row.RowIndex].Value;
    }
    抓住
    {
    返回null;
    }
    }
    
    另一种方法:

    在可绑定对象上创建名为“userSelected”(或类似)的bool属性 在可绑定列表对象上按对象ID创建索引器 在网格上添加可绑定对象的ID作为复选框的属性 On grid_ROWDABOUND还将复选框的checked属性设置为obj.userSelected 在chech_CheckChaged上,使用索引器设置可绑定对象的userSelected属性


    谢谢

    我尝试了@Chuck的预渲染技术,但对我不起作用(VS2005、ASP.net2、SQLsrv2005)。也许技术太旧了,但这是客户所拥有的

    所以我尝试了这个简单的技术,经过一些修改,它工作得非常完美

    我的网格视图

                    <asp:GridView ID="gvFTUNSENT" runat="server" 
                        AutoGenerateColumns="False" CellPadding="4" ForeColor="Black" AllowSorting="False" CssClass="gvCSS" Width="100%"
                        DataKeyNames="StudentID,StudentUnitID" DataSourceID="sdsFTUNSENT" 
                        GridLines="None" AllowPaging="True" PageSize="10" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
                        OnPageIndexChanged="GridView_PageIndexChanged" 
                        OnPageIndexChanging="GridView_PageIndexChanging">
                        <RowStyle Wrap="True" Height="48px" />
                        <Columns>
    ...etc...
                        </Columns>
                        <FooterStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" Height="100%" />
                        <PagerStyle CssClass="cssPager" BackColor="#6B696B" ForeColor="White" HorizontalAlign="Left" Height="100%" />
                        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>
    

    因此,保存和加载方法如下所示:

    private void SavePageState(GridView gv)
    {
        ArrayList categoryIDList = new ArrayList();
        Int32 index = -1;
        foreach (GridViewRow row in gv.Rows)
        {
            HiddenField hfStudentUnitID = (HiddenField)row.FindControl("hfStudentUnitID");
            if (hfStudentUnitID != null)
            {
                if (hfStudentUnitID.Value.Length > 0)
                {
                    index = Convert.ToInt32(hfStudentUnitID.Value.ToString()); //gv.DataKeys[row.RowIndex]["StudentUnitID"];
                    bool result = ((CheckBox)row.FindControl("cbSEND")).Checked;
    
                    // Check in the Session
                    if (Session["CHECKED_ITEMS"] != null)
                        categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
                    if (result)
                    {
                        if (!categoryIDList.Contains(index))
                            categoryIDList.Add(index);
                    }
                    else
                        categoryIDList.Remove(index);
                }
            }
        }
        if (categoryIDList != null && categoryIDList.Count > 0)
            Session["CHECKED_ITEMS"] = categoryIDList;
    }
    

    要使这项工作对您有效,您需要将分页方法调用放入GridView,将复选框ID从cbSEND更改为您正在使用的内容,并将HiddenFields指向具有行唯一标识符的其他控件或值不要使用
    行索引,因为这在GridView中的数据长度上不是唯一的


    工作起来很有魅力

    会话中应该存储什么?选中复选框的gridview的行索引?请尝试我提供的第二个链接,它有一个示例代码,可能会对您有所帮助。谢谢。第二个链接给出了我的答案,搜索中的任何人都可以看到相同的链接。花了点时间,因为我没有时间回到这里,并将其标记为答案,sry。请格式化您的答案,使用enters和```符号强调,例如,物业名称
    protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView gv = (GridView)sender;
    
        //saves a "copy" of the page before it's changed over to the next one
        SavePageState(gv);
    
        gv.PageIndex = e.NewPageIndex;
        gv.DataBind();
    }
    
        protected void GridView_PageIndexChanged(object sender, EventArgs e)
        {
    ...your code to handle anything after the page has changed
    
                        gv.DataSource = dt;
                        gv.DataSourceID = null;
                        gv.DataBind();
    
                        //reload any checkboxes that were session saved in the page
                        LoadPageState(gv);
                    }
                }
            }
        }
    
    private void SavePageState(GridView gv)
    {
        ArrayList categoryIDList = new ArrayList();
        Int32 index = -1;
        foreach (GridViewRow row in gv.Rows)
        {
            HiddenField hfStudentUnitID = (HiddenField)row.FindControl("hfStudentUnitID");
            if (hfStudentUnitID != null)
            {
                if (hfStudentUnitID.Value.Length > 0)
                {
                    index = Convert.ToInt32(hfStudentUnitID.Value.ToString()); //gv.DataKeys[row.RowIndex]["StudentUnitID"];
                    bool result = ((CheckBox)row.FindControl("cbSEND")).Checked;
    
                    // Check in the Session
                    if (Session["CHECKED_ITEMS"] != null)
                        categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
                    if (result)
                    {
                        if (!categoryIDList.Contains(index))
                            categoryIDList.Add(index);
                    }
                    else
                        categoryIDList.Remove(index);
                }
            }
        }
        if (categoryIDList != null && categoryIDList.Count > 0)
            Session["CHECKED_ITEMS"] = categoryIDList;
    }
    
    private void LoadPageState(GridView gv)
    {
        ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
        if (categoryIDList != null && categoryIDList.Count > 0)
        {
            foreach (GridViewRow row in gv.Rows)
            {
                HiddenField hfStudentUnitID = (HiddenField)row.FindControl("hfStudentUnitID");
                if (hfStudentUnitID != null)
                {
                    if (hfStudentUnitID.Value.Length > 0)
                    {
                        Int32 index = Convert.ToInt32(hfStudentUnitID.Value.ToString()); //gv.DataKeys[row.RowIndex]["StudentUnitID"];
                        if (categoryIDList.Contains(index))
                        {
                            CheckBox myCheckBox = (CheckBox)row.FindControl("cbSEND");
                            myCheckBox.Checked = true;
                        }
                    }
                }
            }
        }
    }