每当重定向到另一个页面时,复选框值就会在gridview控件中消失

本文关键字:gridview 消失 控件 复选框 重定向 另一个 | 更新日期: 2023-09-27 18:06:42

我有一个页面,我有gridview和页面索引变化也为每一个记录,我有

一个复选框。在页面的顶部,我有imagebutton当我点击这个按钮时,我将它重定向到另一个页面在该页我有一个后退按钮,重定向到当前页面与复选框和gridview。

我应该做些什么来保留复选框当我选中或其他事情时?

这是gridview分页:

  protected void ManageCalenderShift_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    StoreOldValue();
    EmployeeDetails.PageIndex = e.NewPageIndex;
    SortedBindDataToGrid();
    PupulateoldCheckValue();
}
private void StoreOldValue()
{
    ArrayList categoryIDList = new ArrayList();
    foreach (GridViewRow row in EmployeeDetails.Rows)
    {
        Label can_id = (Label)row.FindControl("UserACENumber");
        bool result = ((CheckBox)row.FindControl("Chkgrid")).Checked;
        if (Session["CHECKED_ITEMS"] != null)
            categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
        if (result)
        {
            if (!categoryIDList.Contains(can_id.Text))
                categoryIDList.Add(can_id.Text);
        }
        else
            categoryIDList.Remove(can_id.Text);
    }
    if (categoryIDList != null && categoryIDList.Count > 0)
        Session["CHECKED_ITEMS"] = categoryIDList;
}
private void PupulateoldCheckValue()
{
    ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
    if (categoryIDList != null && categoryIDList.Count > 0)
    {
        foreach (GridViewRow row in EmployeeDetails.Rows)
        {
            Label can_id = (Label)row.FindControl("UserACENumber");
            if (categoryIDList.Contains(can_id.Text))
            {
                CheckBox myCheckBox = (CheckBox)row.FindControl("Chkgrid");
                myCheckBox.Checked = true;
            }
        }
    }
}

这是指向另一个页面代码的重定向,指向page1:

protected void imgView_Click(object sender, ImageClickEventArgs e)
{
    StoreOldValue();
    PupulateoldCheckValue();
    Response.Redirect("page1.aspx?UserACENumber=" + (Server.UrlDecode(URLSecurity.Encrypt(UserContext.ACENumber))));
}

然后在"page1"我有返回按钮重定向到"page" aspx:

 protected void imgimgBack_Click(object sender, ImageClickEventArgs e)
{
    Response.Redirect("page.aspx?UserACENumber=" + (Server.UrlDecode(URLSecurity.Encrypt(UserContext.ACENumber))));
}

现在我的问题是:

当我选中"页"中的任何一个复选框时。点击图片按钮,重定向到page1。Aspx"并返回到当前工作"页面。Aspx "无论我选中了什么复选框都会消失

每当重定向到另一个页面时,复选框值就会在gridview控件中消失

您必须手动操作。

在GridView中分页时保持CheckBox的状态

当你从另一个页面导航时,当页面加载时,你的GridView正在重新构建。

维护在整个gridview和不同页面中选中的复选框的最佳方法是将这些值保存在某种数据库/存储中。这样,当你绑定GridView时,你就可以相应地设置复选框的值。

另一种方法是在包含GridView的页面上添加一些页面缓存,但是我不确定当您涉及分页时,这将是最佳解决方案。

从我读到的CheckBox不使用ViewState来保留其状态。它使用HTTP POST的内容来确定是否将控件设置为Checked。

因此,解决方案是在数据库/会话等中持久化检查值,然后在页面再次加载时重新绑定它们。

您可以考虑使用Server。转移。我认为你仍然可以通过这种方式访问页面的视图状态。