现在的位置: 首页 > 综合 > 正文

转载 DataList翻页

2012年12月18日 ⁄ 综合 ⁄ 共 4858字 ⁄ 字号 评论关闭
int CurrentPage;//当前页数
  int PageSize;   //每页条数
  int PageCount;  //总页数
  int RecordCount;//
总条数

  
  
private void Page_Load(object sender, System.EventArgs e)
  {
   PageSize=16; //每页为16条记录
   if(!IsPostBack)
   {
    if(Session["ji"]==null||Session["ji"].ToString()=="")
    {
     Response.Write("<script language = javascript>window.open('Login.aspx','_top')</script>");
    }
    else
    {
    
     DataListBind();
    Page.DataBind();
    CalculateRecord();
  
 }
      }

  }

  public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件
  {
   CurrentPage =(int)ViewState["PageIndex"];//获得当前页索引
   PageCount = (int)ViewState["PageCount"];//获得总页数

   int inpage=0;
   string cmd = e.CommandName;

   //判断cmd,以判定翻页方向

   switch (cmd)
   {
    case "prev"://上一页
     if (CurrentPage > 0)CurrentPage--;
     break;

    case "next":
     if (CurrentPage < (PageCount - 1)) CurrentPage++;//
下一页
     break;

    case "first"://第一页
     CurrentPage = 0;
     break;

    case "end"://最后一页
     CurrentPage = PageCount - 1;
     break;

    case "jump"://跳转到第几页
     try
     {
      inpage=int.Parse(TextBox2.Text.Trim());
                 }
     catch
     {
      Response.Write("<script>alert('只能输入数字!!!')</script>");
      return;
     }     
    if (this.TextBox2.Text.Trim()== "" || Int32.Parse(this.TextBox2.Text.Trim()) > PageCount ||Int32.Parse(this.TextBox2.Text.Trim())<0)//如果输入数字为空或超出范围则返回
     {
      return;
     }
     else
     {                   
      CurrentPage = Int32.Parse(this.TextBox2.Text.ToString()) - 1;
      break;
     }
   }
   ViewState["PageIndex"] = CurrentPage;//获得当前页

   //重新将DataList绑定到数据库
//CalculateRecord();
  DataListBind(); 

  }

  public void  CalculateRecord()
  {
              
   //不可以放在初始化条件之前就绑定,那样的话,如果仅有一页的数据,“下一页”页仍然显示
   CurrentPage = 0;//
当前页习惯设为0
   ViewState["PageIndex"] = 0;//页索引也设为0

   //计算总共有多少记录
   RecordCount =com.kecount();
           
   //计算总共有多少页
   if (RecordCount % PageSize == 0)
   {
    PageCount = RecordCount / PageSize;
   }
   else
   {
    PageCount = RecordCount / PageSize + 1;
   }

   this.TotalLbl.Text = PageCount.ToString();//显示总页数
   ViewState["PageCount"] = PageCount;//会话session 对整个 application 有效 ,而视图状态 viewstate相当于某个页面的 session
   this.DataListBind();

  }
  public void DataListBind()
  {

   string zhuang="zhuang";
   int StartIndex = CurrentPage * PageSize;
   if(StartIndex<0)
   {
    StartIndex=0;
   
   }

          DataSet ds=com.kehu1(StartIndex,PageSize,zhuang);
      
   DataList1.DataSource=ds.Tables["zhuang"].DefaultView;
    
   DataList1.DataBind();
   
    this.PreviousLB.Enabled = true;
    this.NextLB.Enabled = true;
    if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//
当为最后一页时,下一页链接按钮不可用
    if (CurrentPage == 0) this.PreviousLB.Enabled = false;//
当为第一页时,上一页按钮不可用
    this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数
   
  }

  private void DataList1_UpdateCommand_1(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
  {
   Response.Redirect("addkefu.aspx?type=2"+"&id="+DataList1.DataKeys[e.Item.ItemIndex].ToString());
  }

  private void DataList1_DeleteCommand_1(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
  {
   
  
             DataTable url=com.kehusele(int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString())).Tables[0];

   ViewState["name"]=Server.MapPath("..\\kehu\\"+url.Rows[0]["name"].ToString());

   bool dele=com.delekehu(int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString()));
   if(dele==true)
   {
    Response.Write("<script>alert('删除成功!!!')</script>");
    FileInfo finfo = new FileInfo(ViewState["name"].ToString());
    if(finfo.Exists)
    {
     finfo.Delete();
    }
   
   }
   else
   {
   Response.Write("<script>alert('删除失败!!!')</script>");
   }
   CalculateRecord();

    DataListBind();
  }

  private void DataList1_ItemDataBound_1(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
  {
   if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
   {
    
    //判断删除按扭
    Button Button2=(Button)e.Item.FindControl("Button2");
    Button2.Attributes.Add("onclick","return confirm('确定要删除吗??')");
    
   }
  }

  
  public String Imger1
  {
   
   get {return _imger1;}
  set {_imger1=value;}
}

 
 }
}

HTML

<td><FONT face="宋体" size="2"><asp:linkbutton id="FirstLB" runat="server" OnCommand="LinkButton_Click" CommandName="first">第一页</asp:linkbutton><asp:linkbutton id="PreviousLB" runat="server" OnCommand="LinkButton_Click" CommandName="prev">上一页</asp:linkbutton><asp:linkbutton id="NextLB" runat="server" OnCommand="LinkButton_Click" CommandName="next">下一页</asp:linkbutton><asp:linkbutton id="EndLB" runat="server" OnCommand="LinkButton_Click" CommandName="end">最后一页</asp:linkbutton>共
       <asp:label id="TotalLbl" runat="server" ForeColor="#FF8000"></asp:label>页 当前第
       <asp:label id="CurrentLbl" runat="server" ForeColor="#FF8000"></asp:label>页
       <asp:linkbutton id="JumpLB" runat="server" OnCommand="LinkButton_Click" CommandName="jump">跳到</asp:linkbutton>第
       <asp:TextBox id="TextBox2" runat="server" Width="63px"></asp:TextBox>页</FONT></td>

 

 

原文地址:http://www.cnblogs.com/aspnet-sqlserver/archive/2007/12/20/1008056.html

抱歉!评论已关闭.