private void dgdList_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
if(ViewState["pagedata"]!=null)
{
Hashtable ht=(Hashtable)ViewState["pagedata"];
if(ht.Contains(this.dgdList.CurrentPageIndex))
{
CheckBox cb=(CheckBox)e.Item.FindControl("chkID");
cb.Checked=ht[this.dgdList.CurrentPageIndex].ToString()[e.Item.ItemIndex].ToString()=="1";    此处报错,“索引超出了数组界限”
}
}
}

此处的datagrid是一个封装好了的dll,该控件已经包含了分页并且包含了一个dropdownlist可以跳转页,我用viewstate来保存每个页里选中的checkbox的状态(从网上找的代码)。现在分页的时候可以保存状态了,但是当我用ddl跳转页的时候,问题就出来了,假设有3页,问题1:当我在第1页选中第一个checkbox,用ddl跳到第2页时,第2页的第一个checkbox也被选中了,其实我并没有选中;问题2:我由第3页跳到第2页或第1页时,就报索引超出了数组界限这个错误,我的第3页的记录数小于前两页每页的记录数。
大家帮忙看看,一起解决这个问题,谢谢。

解决方案 »

  1.   

    翻页多选的问题,也可以用别的方法实现的:http://topic.csdn.net/u/20071224/12/aa98b9be-e474-47f1-b2d0-ad8b3ea89273.html
      

  2.   

    我想先用这种方法把问题解决了,在看看其他的方法。private void dgdList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    string data="";
    foreach(DataGridItem dgi in this.dgdList.Items)
    {
    CheckBox cb=(CheckBox)dgi.FindControl("chkID");
    if(cb.Checked)
    data+="1";
    else
    data+="0";
    }
    if(ViewState["pagedata"]!=null)
    {
    Hashtable ht=(Hashtable)ViewState["pagedata"];
    if(ht.Contains(this.dgdList.CurrentPageIndex))                
    ht[this.dgdList.CurrentPageIndex]=data;
    else
    ht.Add(this.dgdList.CurrentPageIndex,data);
    ViewState["pagedata"]=ht;
    }
    else
    {
    Hashtable ht=new Hashtable();
    ht.Add(this.dgdList.CurrentPageIndex,data);
    ViewState["pagedata"]=ht;
    }
    dgdList.CurrentPageIndex = e.NewPageIndex;
    dgdList.DataBind();
    }
      

  3.   

    不是很明白你的意思,如果是分页时,记录选中的行的话:
    先用循环记录你每页选中的DataGrid索引.DataGrid有个KEY,把你的ID存为KEY,就不会出现第二页又选中的
      

  4.   

    “首页”“前页”“后页”“尾页”点击这四个button没问题,checkbox的状态可以保存,现在就是用drowdownlist跳转页的时候出问题了。
      

  5.   

    不要用viewstate保存
    用一个hidden保存,把每页选中行的ID用特殊字符(比如“|”)隔开,然后在翻页前(比如GridView的PageIndexChanging()事件中)保存入hidden
    最后从hidden中取出即可
      

  6.   

    cb.Checked=ht[this.dgdList.CurrentPageIndex].ToString()...[e.Item.ItemIndex].ToString()=="1";
    这一句有问题吧.
    还有ht是从下标0开始的吧.
    this.dgdList.CurrentPageIndex这个是1开始的吧
      

  7.   

    问题2:用断点调试一下,以前我也遇到这个情况,在DDL的SelectedIndexChanged事件里把DATAGRID的CurrentPageIndex = 0
      

  8.   

    我这个ddl是在datagrid里写好的,已经封装成dll了,我没法修改。
      

  9.   

    一个可能性ht[this.dgdList.CurrentPageIndex].ToString().Length < e.Item.ItemIndex
    关键问题在 ht[this.dgdList.CurrentPageIndex] 这个字符串上
      

  10.   

    cb.Checked=ht[this.dgdList.CurrentPageIndex].ToString()[e.Item.ItemIndex].ToString()=="1";    cb.Checked=(ht[this.dgdList.CurrentPageIndex].ToString()[e.Item.ItemIndex].ToString()=="1"); 是不是应该加括号