国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

Asp.net url分頁的用戶控件

2019-11-18 19:17:19
字體:
供稿:網(wǎng)友

出處:snooker_li的專欄

最近做一個相冊程序頻繁的需要分頁,所以就想寫一個用戶控件出來。

代碼如下:

AutoPage.ascx頁面

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="AutoPage.ascx.cs" Inherits="album.AutoPage" TargetSchema="<table border="0" cellpadding="0" cellspacing="0">
 <tr>
  <td valign="middle" height="30">共<
asp:label id="lb_ItemCount" ForeColor="Red" runat="server"></asp:label>條記錄&nbsp;</td>
  <td valign="middle" height="30"><asp:hyperlink id="hpl_First" runat="server">首頁</asp:hyperlink>&nbsp;</td>
  <td valign="middle" height="30"><asp:hyperlink id="hpl_  <td valign="middle" height="30">當前<asp:label id="lb_CurrentPage" runat="server"></asp:label>頁/共<asp:label id="lb_PageCount" runat="server"></asp:label>頁&nbsp;</td>
  <td valign="middle" height="30"><asp:hyperlink id="hpl_Next" runat="server">下頁</asp:hyperlink>&nbsp;</td>
  <td valign="middle" height="30"><asp:hyperlink id="hpl_Last" runat="server">末頁</asp:hyperlink>&nbsp;</td>
  <td valign="middle" height="30"><asp:textbox id="txb_Page" runat="server" Width="32px" BorderStyle="Solid" BorderWidth="1px"
    BorderColor="Silver"></asp:textbox></td>
  <td valign="middle" height="30"><asp:ImageButton id="btn_go" runat="server" ImageUrl="album_images/go.gif"></asp:ImageButton></td>
  <td valign="middle" height="30"><asp:label id="lb_url" runat="server" Visible="False"></asp:label><asp:Label id="lb_Params" runat="server" Visible="False"></asp:Label></td>
 </tr>
</table>

AutoPage.ascx.cs頁面

namespace album
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;
 using System.Data.SqlClient;

 /// <summary>
 ///  UC 的摘要說明。
 /// </summary>
 public class AutoPage : System.Web.UI.UserControl
 {
  protected System.Web.UI.WebControls.HyperLink hpl_First;
  protected System.Web.UI.WebControls.HyperLink hpl_Prev;
  protected System.Web.UI.WebControls.HyperLink hpl_Next;
  protected System.Web.UI.WebControls.Label lb_CurrentPage;
  protected System.Web.UI.WebControls.Label lb_PageCount;
  protected System.Web.UI.WebControls.HyperLink hpl_Last;
  public int pagesize;
  public string PageP;
  protected System.Web.UI.WebControls.TextBox txb_Page;
  protected System.Web.UI.WebControls.Label lb_url;
  protected System.Web.UI.WebControls.Label lb_ItemCount;
  public string url;
  protected System.Web.UI.WebControls.Label lb_Params;
  protected System.Web.UI.WebControls.ImageButton btn_go;
  public string Params;

  private void Page_Load(object sender, System.EventArgs e)
  {
  
  }

  public PagedDataSource databind(DataTable dt)
  {
   lb_url.Text = url;
   lb_Params.Text = Params;
   //創(chuàng)建分頁類
   PagedDataSource objPage = new PagedDataSource();
   //設置數(shù)據(jù)源
   objPage.DataSource = dt.DefaultView;
   //允許分頁
   objPage.AllowPaging = true;
   //設置每頁顯示的項數(shù)
   objPage.PageSize = pagesize;
   //設置當前頁的索引
   int CurPage=1;
   try
   {
    CurPage = Convert.ToInt32(PageP);
    if (CurPage<1 || CurPage>objPage.PageCount)
    {
     Response.Redirect(url+"?page=1"+Params);
    }
   }
   catch
   {
    Response.Redirect(url+"?page=1"+Params);
   }
   objPage.CurrentPageIndex = CurPage-1;
   //顯示狀態(tài)信息
   lb_ItemCount.Text = dt.Rows.Count.ToString();
   lb_CurrentPage.Text = CurPage.ToString();
   lb_PageCount.Text =objPage.PageCount.ToString();
    
   //如果當前頁面不是首頁
   if (!objPage.IsFirstPage)
   {
    hpl_Prev.NavigateUrl=url + "?Page=" + Convert.ToString(CurPage-1)+Params;
    hpl_First.NavigateUrl=url + "?Page=1"+Params;
   }
   //如果當前頁面不是最后一頁
   if (!objPage.IsLastPage)
   {
    hpl_Next.NavigateUrl=url+ "?Page=" + Convert.ToString(CurPage+1)+Params;
    hpl_Last.NavigateUrl=url + "?Page=" +objPage.PageCount.ToString()+Params;
   }
   return objPage;
  }


  #region Web 窗體設計器生成的代碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調(diào)用是
asp.net Web 窗體設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
 
  /// <summary>
  ///  設計器支持所需的方法 - 不要使用代碼編輯器
  ///  修改此方法的內(nèi)容。
  /// </summary>
  private void InitializeComponent()
  {
   this.btn_go.Click += new System.Web.UI.ImageClickEventHandler(this.btn_go_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void btn_go_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   Response.Redirect(lb_url.Text+"?Page="+txb_Page.Text+lb_Params.Text);
  }

 
 }
}

調(diào)用的時候需要設置幾個參數(shù)pagesize(每頁顯示數(shù)據(jù)個數(shù)),PageP(傳遞的分頁參數(shù)),ParmP(其他的Request.QureyString參數(shù)),url(頁面地址)

綁定的時候只需要把控件的DataSource=AutoPage1.databind(DataTable變量)


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 辰溪县| 广东省| 襄垣县| 大余县| 和政县| 云南省| 筠连县| 海阳市| 灵宝市| 巴塘县| 新宁县| 天水市| 新河县| 东光县| 荔浦县| 雷州市| 苍梧县| 五台县| 陆川县| 巴楚县| 罗平县| 远安县| 呼图壁县| 陆良县| 湟中县| 吉水县| 盐城市| 东阿县| 滕州市| 衢州市| 石城县| 延寿县| 金秀| 丰都县| 淮阳县| 收藏| 如东县| 定南县| 沂水县| 石首市| 闽侯县|