商务合作:179001057@qq.com

Control Study -> 自定义DataGrid翻页按钮(示例代码下载)

技术2022-05-11  0


某平台价值19860元的编程课程资料免费领取【点我领取】


(一).显示截图效果

  

(二).代码

   1. *.aspx文件代码

         <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="New_DataGrid自定义分页按钮.WebForm1" %>   WebForm1            http://schemas.microsoft.com/intellisense/ie5">    

            runat="server" Width="768px" AllowPaging="True">                 
 

 2. *.cs代码文件

    using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;

namespace New_DataGrid自定义分页按钮{ ///

 /// DataGrid自定义分页按钮 ///  public class WebForm1 : System.Web.UI.Page  {   protected System.Web.UI.WebControls.DataGrid DataGrid1;              private void Page_Load(object sender, System.EventArgs e)   {       if(!Page.IsPostBack)    {     GetData();        // 生成数据     this.BindData();  // 绑定数据到DataGrid        }        }

  ///

  /// 生成数据  ///   private void GetData()   {    DataTable table = new DataTable();    DataColumnCollection columns = table.Columns;

   //定义结构   columns.Add("学号",typeof(System.Int32));   columns.Add("分数",typeof(System.Int32));        Random myRandom = new Random();   int intScore = 0;   for(int i = 1;i < 101; i++)   {                    intScore = myRandom.Next(100);    DataRow drNew = table.NewRow();    drNew[0] = i.ToString();    drNew[1] = intScore.ToString();    table.Rows.Add(drNew);   }   this.ViewState.Add("table",table);     }

  ///

  /// 绑定DataGrid  ///   private void BindData()   {       DataTable table = (DataTable)this.ViewState["table"];    this.DataGrid1.DataSource = table;    this.DataGrid1.DataBind();   }     

  #region Web 窗体设计器生成的代码  override protected void OnInit(EventArgs e)  {   //   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。   //   InitializeComponent();   base.OnInit(e);  }    ///

  /// 设计器支持所需的方法 - 不要使用代码编辑器修改  /// 此方法的内容。  ///   private void InitializeComponent()   {       this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);    this.Load += new System.EventHandler(this.Page_Load);    this.DataGrid1.ItemCreated +=new DataGridItemEventHandler(DataGrid1_ItemCreated);

  }  #endregion

  private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)  {   try   {    this.DataGrid1.CurrentPageIndex = e.NewPageIndex;   }   catch   {    this.DataGrid1.CurrentPageIndex = 0;   }   this.BindData();  }

  private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)  {   System.Web.UI.WebControls.ListItemType elemType = e.Item.ItemType;   if (elemType == System.Web.UI.WebControls.ListItemType.Pager)    {    TableCell pager = (TableCell) e.Item.Controls[0];    for (int i=0; i     {      Object o = pager.Controls[i];      if (o is LinkButton)      {       LinkButton h = (LinkButton) o;       h.Text = " " + h.Text + " ";      }      else      {       Label l = (Label) o;       l.Text = String.Format(" [第{0}页]", l.Text);      }     }    }   }  }    }

(三).源代码下载(创建虚拟目录后就可以直接运行)

    http://www.cnblogs.com/Files/ChengKing/自定义分页example.rar

    

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=533551


最新回复(0)