时间:2021-05-28
一、.ascx页面
复制代码 代码如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Pagination.ascx.cs" Inherits="IOCS.WEB.UserControl.Pagination" %>
<link href="../Content/Css/Pager.css" rel="stylesheet" type="text/css" />
<div id="tbPage" class="pager" runat="server" >
記錄總數:<asp:Label ID="LRecords" runat="server"></asp:Label>
總頁數:<asp:Label ID="LPages" runat="server"></asp:Label>
當前頁:<asp:Label ID="LPage" runat="server"></asp:Label>
<asp:LinkButton ID="LinkFirst" runat="server" CommandArgument="first" nClick="PagerButtonClick"
Text="首頁"></asp:LinkButton>
<asp:LinkButton ID="LinkPrevious" runat="server" CommandArgument="prev" nClick="PagerButtonClick"
Text="上一頁"></asp:LinkButton>
<asp:LinkButton ID="LinkNext" runat="server" CommandArgument="next" nClick="PagerButtonClick"
Text="下一頁"></asp:LinkButton>
<asp:LinkButton ID="LinkLast" runat="server" CommandArgument="last" nClick="PagerButtonClick"
Text="末頁"></asp:LinkButton>
轉到第<asp:TextBox ID="txtpage" CssClass="piut" runat="server" MaxLength="5" AutoPostBack="True" nTextChanged="txtpage_TextChanged"></asp:TextBox>頁
二、.ascx.cs文件
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace IOCS.WEB.UserControl
{
public partial class Pagination : System.Web.UI.UserControl
{
public event EventHandler PageButtonClick;
public bool FirstPost = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
// 只輸入數字
txtpage.Attributes.Add("onclick",
@"if(!((event.keyCode>=48&&event.keyCode<=57)
||(event.keyCode>=96&&event.keyCode<=105)
||(event.keyCode==8)))event.returnValue=false;"
);
}
GridView _gv;
/// <summary>
/// 需要分頁的GridView
/// </summary>
public GridView TargetControlID
{
set
{
_gv = value;
}
get
{
return _gv;
}
}
protected void PagerButtonClick(object sender, EventArgs e)
{
//獲得linkebutton的參數值
string arg = ((LinkButton)sender).CommandArgument;
switch (arg)
{
case ("next"):
{
if (_gv.PageIndex < _gv.PageCount - 1)
{
_gv.PageIndex=_gv.PageIndex+1;
}
break;
}
case ("prev"):
{
if (_gv.PageIndex > 0)
{
_gv.PageIndex--;
}
break;
}
case ("first"):
{
_gv.PageIndex = 0;
break;
}
case ("last"):
{
if (_gv.PageCount > 0)
{
_gv.PageIndex = _gv.PageCount - 1;
}
break;
}
default:
{
_gv.PageIndex = Convert.ToInt32(arg);
break;
}
}
PageButtonClick(sender, e);
}
public void SetPageButton()
{
if (_gv.PageIndex == 0)
{
LinkFirst.Enabled = false;
LinkPrevious.Enabled = false;
LinkFirst.Style["color"] = "gray";
LinkPrevious.Style["color"] = "gray";
object s = LinkFirst.Style.Keys;
if (_gv.PageCount > 1)
{
LinkNext.Enabled = true;
LinkLast.Enabled = true;
txtpage.Enabled = true;
txtpage.Enabled = true;
LinkNext.Style["color"] = "#000";
LinkLast.Style["color"] = "#000";
txtpage.Style["readonly"] = "false";
}
else
{
LinkNext.Enabled = false;
LinkLast.Enabled = false;
txtpage.Enabled = false;
LinkNext.Style["color"] = "gray";
LinkLast.Style["color"] = "gray";
txtpage.Style["readonly"] = "true";//background-color
}
}
else if (_gv.PageIndex == _gv.PageCount - 1)
{
LinkFirst.Enabled = true;
LinkPrevious.Enabled = true;
LinkNext.Enabled = false;
LinkLast.Enabled = false;
LinkFirst.Style["color"] = "#000";
LinkPrevious.Style["color"] = "#000";
LinkNext.Style["color"] = "gray";
LinkLast.Style["color"] = "gray";
}
else
{
LinkFirst.Enabled = true;
LinkPrevious.Enabled = true;
LinkNext.Enabled = true;
LinkLast.Enabled = true;
LinkFirst.Style["color"] = "#000";
LinkPrevious.Style["color"] = "#000";
LinkNext.Style["color"] = "#000";
LinkLast.Style["color"] = "#000";
}
}
/// <summary>
/// 設定頁面信息
/// </summary>
/// <param name="dsCount">DataSet的紀錄總數</param>
public void SetPageRecord(int dsCount)
{
LRecords.Text = dsCount.ToString();
int mod= dsCount%_gv.PageSize;
LPages.Text = (mod == 0 ? dsCount / _gv.PageSize : dsCount / _gv.PageSize + 1).ToString();
LPage.Text = (_gv.PageIndex + 1).ToString();
tbPage.Visible = true;
SetPageButton();
}
protected void txtpage_TextChanged(object sender, EventArgs e)
{
if (txtpage.Text != "")
{
try
{
int index = int.Parse(txtpage.Text.Trim());
if (index <= _gv.PageCount && index >= 1)
{
_gv.PageIndex = index - 1;
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "<script. language='javascript'>alert('對不起,頁數超過索引范圍!');</script>");
}
}
catch
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "<script. language='javascript'>alert('對不起,只能輸入數字!');</script>");
}
}
PageButtonClick(sender, e);
}
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
功能:可以把某个asp.net控件替换成自定义的控件pages的tagMapping元素(ASP.NET设置架构)定义一个标记类型的集合,这些标记类型在编译时重
本文实例讲述了ASP.NET自定义Web服务器控件之Button控件实现方法。分享给大家供大家参考。具体实现方法如下:复制代码代码如下:usingSystem;
在实现asp.net的自定义控件中,若要实现数据的回发或者post数据,那自义控件必须实现IPostBackDataHandler接口,在该接口中有两个方法一个
一、AspNetPager分页控件分页是Web应用程序中最常用到的功能之一,在ASP.NET中,虽然自带了一个可以分页的DataGrid(asp.net1.1)
ASP.NET中用GridView时,常常用到自定义模板(因为默认的实在不能满足我们的需求),而自定义时,往往少不了Eval来输出字段值。 但Eval的内