2

我有一个包含 4 列的网格视图,包括一个作为 Approve 的列。Approve 有一个复选框,它将在绑定后对数据集的所有行重复。我已将 allowpaging = true & pageindexsize 设为 10。现在,假设我选中第 2 行和第 5 行的复选框,然后移至第 2 页,然后返回第 1 页,即我选中的复选框(第 2 行和第 5 行)第 1 页正在重置为未选中状态。我知道原因,这是由于我们在 onpageindexchange 事件上所做的 gridview 的绑定。但是,即使我们从一页移动到另一页,有什么方法可以保持复选框的状态。

谢谢

4

4 回答 4

1

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

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

  1. http://forums.asp.net/p/1368550/2852029.aspx
  2. http://myaspsnippets.blogspot.com/2010/08/maintaining-state-of-checkboxes-while.html
于 2011-06-28T04:50:10.857 回答
1

试试看,(您必须将两个事件 Checkbox_Checked 和 Checkbox_PreRender 设置为网格中的 CheckBox 控件。此外,必须在网格中设置 DataKey,以便在 Checks 数组中建立索引。)

protected bool[] Checks
{
    get { return (bool[])(ViewState["Checks"] ?? new bool[totalLengthOfDataSource]); }
    set { ViewState["Checks"] = value; }
}

protected void Checkbox_Checked(object sender, EventArgs e)
{
    CheckBox cb = (CheckBox)sender;
    bool[] checks = Checks;
    checks[(int)GetRowDataKeyValue(sender)] = cb.Checked;
    Checks = checks;
}

protected void Checkbox_PreRender(object sender, EventArgs e)
{
    CheckBox cb = (CheckBox)sender;
    bool[] checks = Checks;
    cb.Checked = checks[(int)GetRowDataKeyValue(sender)];
}

这些在静态 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;
    }
}
于 2011-06-28T07:17:53.767 回答
0

另一种方法:

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

谢谢

于 2013-11-13T14:16:51.297 回答
0

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

所以我尝试了简单的myaspsnippets技术,并通过一些修改它完美地工作!

我的网格视图

                <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>

我使用的分页方法是:

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);
                }
            }
        }
    }

所以 SAVE 和 LOAD 方法如下:

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;
                    }
                }
            }
        }
    }
}

为了使这项工作适合您,您需要将 Paging 方法调用放入您的 GridView,将 CheckBox ID 从 cbSEND 更改为您使用的任何内容,并将 HiddenFields 指向其他具有唯一标识符的控件或值为您的行。不要使用RowIndex,因为它在 GridView 中的整个数据长度中并不是唯一的。

奇迹般有效!

于 2017-03-30T01:19:40.010 回答