protected void BindToBBS_(int pageIndex,string TitleCou)
    {
        int n = pageIndex * int.Parse(TitleCou);
        int m = (pageIndex - 1) * int.Parse(TitleCou);
        string sql = "select top " + TitleCou + " BBS_ID,BBS_title as title,BBS_addtime as times,Auid=(select UserID from [user] where UserName=BBS.BBS_author),";
        sql += "reply=(select count(*) from Reply where Reply.bbs_ID =BBS.BBS_ID),Ruid=(select UserID from [User] where UserName=(select top 1 reply_author";
        sql+=" from reply where reply.BBS_ID = BBS.BBS_ID order by reply_addtime desc)),";
                sql+="BBS_author as author,zonename=(select zone_name from zone where zone_ID=BBS.Zone_ID),";
                sql+="BBS_Click as click,BBS_tags as tags,last=(select top 1 reply_author from reply where reply.BBS_ID = BBS.BBS_ID ";
                sql+="order by reply_addtime desc),lasttime=(select top 1 reply_addtime from reply where reply.BBS_ID = BBS.BBS_ID ";
                sql+="order by reply_addtime desc),zoneID=(select zone_ID from zone where Zone.zone_ID=BBS.Zone_ID),BBS_Click as click from ";
                sql+="bbs where BBS_ID in (select top "+n+" BBS_ID from BBS order by BBS_ID desc) and BBS_ID ";
                sql += "not in (select top "+m+" BBS_ID from BBS order by BBS_ID desc) order by BBS_ID desc";            
                this.DataList1.DataSource = cn.DataSets(sql);
                this.DataList1.DataKeyField = "BBS_ID";
                this.DataList1.DataBind();
        
  
    }
上面的是绑定Datalist。我下面在ItemDatabind里
 
        //String bID = DataBinder.Eval(e.Item.DataItem, "BBS_ID").ToString();
        string bID = this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
        HtmlTable dpost = (HtmlTable)this.DataList1.FindControl("dlpage");
        int Cou = cn.ok("select Count(*) from reply where bbs_id=" + int.Parse(bID));
        int p1 = (Cou) % 10;
        int p = (Cou - p1) / 10 + 1; 说索引超出范围。必须为非负值并小于集合大小。
参数名: index,我看了一下 his.DataList1.DataKeys.Count 为空 可是,Datalist里确实有东西的。不用IteMdatabind的时候能正常绑定并显示
为什么呢,

解决方案 »

  1.   

    你设置  DataList 的 DataKeyFiled 这个属性?
      

  2.   

    DataList1.DataKeys[e.Item.ItemIndex].ToString();可能放在DataBind事件中吗?
      

  3.   

    说索引超出范围。必须为非负值并小于集合大小。
    参数名: index 但是 this.Response.Write(this.DataList1.DataKeys.Count.ToString()); 的时候却写出了123456789 等 
      

  4.   

    我发现问题了,第一次运行的时候 Datakeys 为空。。
      

  5.   

    if (this.DataList1.DataKeys.Count!=0)
            {
               DataList1.DataKeys[e.Item.ItemIndex].ToString();}