首页 新闻 搜索 专区 学院

repeater控件分页,如何保存复选框的状态呢?

0
悬赏园豆:20 [已解决问题] 解决于 2011-12-14 14:47

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.BLL;
using Maticsoft.Model;

public partial class test_testcheckbox : System.Web.UI.Page
{
    TestBLL testBLL = new TestBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            doSearch();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string stuNo_all = string.Empty;

        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                CheckBox cb = item.FindControl("chbSelect") as CheckBox;
                if (cb.Checked)
                {
                    stuNo_all += cb.Text + ",";
                }
            }
        }

        string[] stuno = stuNo_all.Split(',');
        for (int i = 0; i < stuno.Length; i++)
        {
            if (stuno[i] != "")
            {
                if (stuno[i] == "1")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "alert('测试数据不能删除!');", true);
                }
                else
                {
                    Test testModel = testBLL.GetModel(Convert.ToInt16(stuno[i]));
                    testModel.stuno = Convert.ToInt16(stuno[i]);
                    testModel.delflag = 1;
                    testBLL.Update(testModel);
                }
            }
        }
        doSearch();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        doSearch();
    }

    private void doSearch()
    {
        this.Repeater1.DataSource = null;
        this.Repeater1.DataBind();
        AspNetPager1.RecordCount = 0;

        DataSet ds = testBLL.GetList("delflag=0");
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = ds.Tables[0].DefaultView;
            pds.AllowPaging = true;
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            pds.PageSize = AspNetPager1.PageSize;
            this.Repeater1.DataSource = pds;
            this.Repeater1.DataBind();
        }
    }
}

 

      大家好,这个是我分页的代码,就是不知道如何保存复选框的状态。我简单把问题描述下,我在第一页选中2个复选框,然后点下一页,再回到第一页,刚刚选中的两个复选框就丢失了,也就是页面刷新了。不知道该怎么解决,麻烦各位老师帮我看下。谢谢了。

i surrender的主页 i surrender | 初学一级 | 园豆:192
提问于:2011-12-13 13:36
< >
分享
最佳答案
1

你把勾选上的复选框对应的数据行ID保存在session中,每次显示的时候做下判断需不需要勾选。

也可以把ID保存在cookies中,用javascript做判断显示。

收获园豆:20
LCM | 大侠五级 |园豆:6876 | 2011-12-13 13:44

老师您好,麻烦可以说的详细一点吗?把ID保存在session中,我写在哪个事件里面呢?可以帮我在我的代码上改改吗?谢谢了

i surrender | 园豆:192 (初学一级) | 2011-12-13 13:46

@i surrender: 我给你写个服务器端用session保存的吧。我就简单地写一下,马上发上来。

LCM | 园豆:6876 (大侠五级) | 2011-12-13 15:03

@i surrender: 后台代码如下 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
doSearch(1);
}
}

private void doSearch(int page)
{
this.Repeater1.DataSource = null;
this.Repeater1.DataBind();

TestData testData=new TestData();
List<Model> list = testData.GetData(page);

this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}


//换页,相当于你页面改变的事件.我这里没有AspNetPager1控件,我换页就直接手写数字换页
protected void Button1_Click1(object sender, EventArgs e)
{
#region 这里就是把选中的checkbox中的Text保存起来,也可以是其它值,反正要能够通过这个值唯一地确定一个checkbox
List<string> checkedModelIdList;
var obj = Session["CheckedModelIdList"];
if (obj == null || (obj as List<string>)==null)
checkedModelIdList=new List<string>();
else
{
checkedModelIdList = obj as List<string>;
}

//换页的时候先保存已选择的checkbox的ID
foreach (RepeaterItem item in Repeater1.Items)
{
CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
if (chk.Checked)
{
if(!checkedModelIdList.Contains(chk.Text))
{
checkedModelIdList.Add(chk.Text);
}
}
else
{
if (checkedModelIdList.Contains(chk.Text))
{
checkedModelIdList.Remove(chk.Text);
}
}
}
Session["CheckedModelIdList"] = checkedModelIdList;
#endregion

//显示新的页
int page = 0;
int.TryParse(TextBox1.Text.Trim(), out page);
doSearch(page);
}
///<summary>
/// 绑定数据时做判断,再设置checkbox的选中状态
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
List<string> checkedModelIdList;
var obj = Session["CheckedModelIdList"];
if (obj == null || (obj as List<string>) == null)
checkedModelIdList = new List<string>();
else
{
checkedModelIdList = obj as List<string>;
}
CheckBox chk = (CheckBox)e.Item.FindControl("CheckBox1");
chk.Checked = checkedModelIdList.Contains(chk.Text);
}
}

///<summary>
/// 这个类是模拟数据的
///</summary>
public class TestData
{
public List<Model> testDataList = new List<Model>();

public TestData()
{
testDataList.Clear();
testDataList.Add(new Model {ModelId = "1", ModelContent = "1axaa"});
testDataList.Add(new Model {ModelId = "2", ModelContent = "1aavca"});
testDataList.Add(new Model {ModelId = "3", ModelContent = "1aacxza"});
testDataList.Add(new Model {ModelId = "4", ModelContent = "1a2aa"});
testDataList.Add(new Model {ModelId = "5", ModelContent = "1abzxaa"});
testDataList.Add(new Model {ModelId = "6", ModelContent = "1abaa"});
testDataList.Add(new Model {ModelId = "7", ModelContent = "1aa a"});
testDataList.Add(new Model {ModelId = "8", ModelContent = "1acxaa"});
testDataList.Add(new Model {ModelId = "9", ModelContent = "1acaa"});
testDataList.Add(new Model {ModelId = "10", ModelContent = "1aaza"});
testDataList.Add(new Model {ModelId = "11", ModelContent = "1aaca"});
testDataList.Add(new Model {ModelId = "13", ModelContent = "1aavca"});
testDataList.Add(new Model {ModelId = "14", ModelContent = "1aaa"});
testDataList.Add(new Model {ModelId = "15", ModelContent = "1azaa"});
testDataList.Add(new Model {ModelId = "16", ModelContent = "1acaa"});
testDataList.Add(new Model {ModelId = "17", ModelContent = "1aacxa"});
testDataList.Add(new Model {ModelId = "18", ModelContent = "1aaccaa"});
testDataList.Add(new Model {ModelId = "19", ModelContent = "1a2ccsn5aa"});
testDataList.Add(new Model {ModelId = "20", ModelContent = "1weaaa"});
}

///<summary>
/// 传入页返回指定页的数据.我假设每页是显示4条数据的.
///</summary>
///<param name="page"></param>
///<returns></returns>
public List<Model> GetData(int page)
{
return testDataList.Skip(4*(page - 1)).Take(4).ToList();
}
}

///<summary>
/// 显示用的实体
///</summary>
public class Model
{
public string ModelId;
public string ModelContent;
}

前台代码如下:

<%@ Page Title="主页" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" Text='<%# ((Model)Container.DataItem).ModelId%>' runat="server" />
<asp:Label ID="Label1" runat="server" Text='<%# ((Model)Container.DataItem).ModelContent%>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
<br/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1"
Text="Button" />
</asp:Content>
LCM | 园豆:6876 (大侠五级) | 2011-12-13 15:08

@i surrender: 代码如上。你可以在界面上的文本框中输入页码,然后点击按钮切换页面。我机器上没有你的分页控件,就用这样模拟一下。用javascript实现的我就不写了。

LCM | 园豆:6876 (大侠五级) | 2011-12-13 15:12

@LCM: 嗯 谢谢 我先看看 不明白的地方再请教您

i surrender | 园豆:192 (初学一级) | 2011-12-13 18:49

@LCM: 老师在您的Repeater1_ItemDataBound事件里面是不是应该加一句if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)呢?

i surrender | 园豆:192 (初学一级) | 2011-12-13 19:07

@i surrender: 加没加应该没什么关系吧。我只是做了个示例,怎么用怎么改就看你自己的需要了。另外,楼下面说的用viewstate也挺好的。只是原理都一样,都是得把已选中checkbox保存起来。

LCM | 园豆:6876 (大侠五级) | 2011-12-14 09:03
其他回答(4)
0

无解,一般都不会这样做分页的批量操作。

 

无论保持在session或cookies中,都会有问题。

walen | 园豆:429 (菜鸟二级) | 2011-12-13 14:10
0

像这样保存:

 Dictionary<int, decimal> dict = (Dictionary<int, decimal>)ViewState["products"];

        foreach (GridViewRow row in this.gvVoucher.Rows)
        {
            KeyValuePair<int, decimal> item = dict.ElementAt<KeyValuePair<int, decimal>>(row.DataItemIndex);

            TextBox tbx = (TextBox)row.Cells[6].FindControl("txtCount");
            decimal result = 0m;
            if (Decimal.TryParse(tbx.Text, out result))
            {
                dict[item.Key] = result;
            }
        }

悟行 | 园豆:12422 (专家六级) | 2011-12-13 15:19
0

像这样的你可以模仿viewstate,保存就可以了

小小刀 | 园豆:1991 (小虾三级) | 2011-12-13 23:33
0

怎么貌似不行啊?到底可不可以啊

静听风吟111 | 园豆:199 (初学一级) | 2012-08-18 14:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册