本帖最后由 刪除記忆ご 于 2012-12-16 10:41 编辑
自己整理的分页代码,共享一下,大家可以看一下!如果有更好的方法,希望告诉我,大家互相交流一下!
方法一:
**.aspx代码:- <asp:DataGrid ID="dgNotice" runat="server">
- <Columns>
- ………………
- </Columns>
- <FONT color=red><PagerStyle Mode="NumericPages" />
- </FONT></asp:DataGrid>
复制代码 **.aspx.cs代码:- protected void dgNotice_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
- {
- dgNotice.CurrentPageIndex = e.NewPageIndex;//dgnotice为控件ID
- NoticeDataBind();//datagrid中显示数据
- }
复制代码 方法二:
1. 在项目中添加AspNetPager.dll引用。
2.**.aspx代码:- <asp:GridView ID="GridView1" Width="80%" runat="server" AutoGenerateColumns="False">
- <Columns>
- <asp:BoundField DataField="ID" HeaderText="ID" />
- <asp:BoundField DataField="KHID" HeaderText="客户ID" />
- </Columns>
- </asp:GridView>
- <webdiyer:aspnetpager id="AspNetPager1" runat="server" ShowCustomInfoSection="Left" UrlPaging="true" ShowBoxThreshold="5" PageSize="25"
- pagingbuttontype="Image" width="80%" ImagePath="../../Style/images/pager/"
- ButtonImageNameExtension="n" ButtonImageExtension=".gif" AlwaysShow="true"
- DisabledButtonImageNameExtension="g" NumericButtonTextFormatString="[{0}]"
- CpiButtonImageNameExtension="r" PagingButtonSpacing="10px" ButtonImageAlign="left"
- OnPageChanged="AspNetPager1_PageChanged">
- </webdiyer:aspnetpager>
复制代码 3.***.aspx.cs代码:- protected void Page_Load(object sender, EventArgs e)
- {
- GridViewBind();
- }
- private void GridViewBind()
- {
- </FONT> SqlConnection con = new SqlConnection("server=128.1.3.68;database=XYGL;uid=hatest;pwd=hatest");<FONT style="BACKGROUND-COLOR: #f7f7f7">
- <FONT style="BACKGROUND-COLOR: #f7f7f7"></FONT><FONT style="BACKGROUND-COLOR: #f7f7f7"></FONT>SqlCommand cmd = new SqlCommand();
- cmd.Connection = con;
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.CommandText = "P_newpager";
- int pageindex = 0;
- string strWhere = "";
- if (AspNetPager1.CurrentPageIndex < 1)
- {
- pageindex = 1;
- }
- else
- {
- pageindex = AspNetPager1.CurrentPageIndex;
- }
- SqlParameter[] parameters ={
- new SqlParameter("@tblname",SqlDbType.VarChar,255),
- new SqlParameter("@strGetFields",SqlDbType.VarChar,1000),
- new SqlParameter("@fldName",SqlDbType.VarChar,255),
- new SqlParameter("@PageSize",SqlDbType.Int,4),
- new SqlParameter("@PageIndex",SqlDbType.Int,4),
- new SqlParameter("@doCount",SqlDbType.Bit),
- new SqlParameter("@OrderType",SqlDbType.Bit),
- new SqlParameter("@strWhere",SqlDbType.VarChar,2000) };
- parameters[0].Value = "usiKHKHDJ";//表或视图名
- parameters[1].Value = "ID,JSID,KHID,KHDM,MC,DLRQ";//显示字段
- parameters[2].Value = "ID";//以某字段排序
- parameters[3].Value = AspNetPager1.PageSize;//多少条为一页
- parameters[4].Value = pageindex;//索引页
- parameters[5].Value = 0;//默认0就行了
- parameters[6].Value = 1;//这里的1是降序,0为升序
- parameters[7].Value = strWhere;//查询条件组合
- foreach (SqlParameter p in parameters)
- {
- cmd.Parameters.Add(p);
- }
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- sda.Fill(ds);
- //动态设置用户自定义文本内容
- AspNetPager1.RecordCount = int.Parse(ds.Tables[1].Rows[0][0].ToString());
- AspNetPager1.CustomInfoHTML = "共" + AspNetPager1.RecordCount.ToString() + "条,";
- AspNetPager1.CustomInfoHTML += "" + AspNetPager1.PageCount.ToString() + "页,";
- AspNetPager1.CustomInfoHTML += "第" + AspNetPager1.CurrentPageIndex.ToString() + "页";
- GridView1.DataSource = ds.Tables[0].DefaultView;
- GridView1.DataBind();
- }
- protected void AspNetPager1_PageChanged(object sender, EventArgs e)
- {
- GridViewBind();
- }
复制代码
|