asp.net RadGrid分页功能扩展Demo

简介: 1、在权限判断代码后面增加如下代码     VSDataTable1 = new DataSet();         public void BindRadGrid()    {             //.
1、在权限判断代码后面增加如下代码

     VSDataTable1 = new DataSet();

    

    public void BindRadGrid()

    {

             //...

             this.RadGrid1.DataSource = ds;
            this.RadGrid1.DataBind();
            ViewState["ajbb"] = ds;

    }

    //RadGrid分页功能扩展代码
    protected void RadGrid1_PageIndexChanged(object source, Telerik.WebControls.GridPageChangedEventArgs e)
    {
        //分页代码
        this.RadGrid1.DataSource = VSDataTable1;
        this.RadGrid1.CurrentPageIndex = e.NewPageIndex;
        this.RadGrid1.DataBind();
    }

 

    #region 分页功能扩展 2012-06-26

   

    public void btnHidden_Click(object sender, EventArgs e)
    {
        int currentPageIndex = int.Parse(hf.Value);
        this.RadGrid1.DataSource = VSDataTable1;
        this.RadGrid1.CurrentPageIndex = currentPageIndex-1;
        this.RadGrid1.DataBind();
    }

    #endregion


   #region 分页通用代码

   /// <summary>
    /// 存放在ViewState的DataTable
    /// </summary>
    private DataSet VSDataTable1
    {
        get { return ViewState["ajbb"] as DataSet; }
        set { ViewState["ajbb"] = value; }
    }

    /// <summary>
    /// 普通的绑定资料
    /// </summary>
    private void PageDataBind()
    {
        this.RadGrid1.DataSource = VSDataTable1;
        this.RadGrid1.DataBind();
    }

    /// <summary>
    /// 绑定资料加换页
    /// </summary>
    /// <param name="getPageIndex">新页面Index</param>
    private void PageDataBind(Int32 getPageIndex)
    {
        this.RadGrid1.DataSource = VSDataTable1;
        this.RadGrid1.CurrentPageIndex = getPageIndex;
        this.RadGrid1.DataBind();
    }

   #endregion


2、前台页面中
将RadGrid中的<PagerStyle/>替换成"></PagerStyle>
<PagerStyle Mode="NextPrevAndNumeric" NextPagesToolTip="下一页" NextPageText="下一页" NextPageToolTip="下一页"
                                            PagerTextFormat="分页&nbsp;:&nbsp;{4} &nbsp;<input type='text' name='txtNum' id='txtNum' value='1' style='width:20px'/><input type='button' id='btnConfirm' value='确定'  onclick='splitPageByHand();'/>"
                                            PrevPagesToolTip="上一页" PrevPageText="上一页" PrevPageToolTip="上一页"></PagerStyle>

3、在</rad:Grid>后面增加2个控件,代码如下:
<asp:HiddenField ID="hf" runat="server" />

<asp:Button ID="btnHidden" runat="server" Style="display: none;" OnClick="btnHidden_Click" />


4、在</html>标记后面添加如下js代码
<!--通用手动分页代码-->
<script type="text/javascript">

    //手动填写页码进行分页跳转
    function splitPageByHand() {
        if (!isNaN(document.getElementById("txtNum").value)) {
            if (document.getElementById("txtNum").value > parseInt('<%=RadGrid1.PageCount%>') || document.getElementById("txtNum").value <= 0) {
                alert('输入数字不在范围内!');
                document.getElementById("txtNum").value = "1";
            }
            else {
                document.getElementById("hf").value = document.getElementById("txtNum").value;
                document.getElementById("btnHidden").click();
            }
        }
        else {
            alert('请输入数字!');
            document.getElementById("txtNum").value = "1";
        }

    }

    //为input控件重新赋值
    if(document.getElementById("txtNum")!=null){
      document.getElementById("txtNum").value = '<%=RadGrid1.CurrentPageIndex+1 %>';
    }
</script>


以下是代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="aqsw_aqsg_list.aspx.cs" Inherits="AQJDGL_aqsw_aqsg_list"
	ValidateRequest="false" %>

<%@ Register Assembly="RadGrid.Net2" Namespace="Telerik.WebControls" TagPrefix="radG" %>
<%@ Register Src="../USerControl/Export.ascx" TagName="Export" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title>安全事故列表</title>
	<link href="../css/PagePartCss.css" type="text/css" rel="stylesheet" />
	<link href="../css/table.css" rel="stylesheet" type="text/css" />
	<link href="../css/channl_css.css" rel="stylesheet" type="text/css" />
	<script src="../public/JS/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
	<style type="text/css">
		div.title
		{
			text-align: right;
			font-size: medium;
			font-weight: bold;
			color: Red;
		}
		#eWebEditor1
		{
			width: 516px;
		}
		.style4
		{
			width: 52px;
		}
		.style5
		{
			width: 111px;
		}
		.style6
		{
			width: 113px;
		}
		.style7
		{
			width: 115px;
		}
		.style8
		{
			width: 24px;
		}
		.style9
		{
			width: 121px;
		}
	</style>
</head>
<body>
	<form id="form1" runat="server">
	<div class="right">
		<div style="text-align: center;" runat="server" id="title">
			<b class="bt">安全事故管理</b>
		</div>
		<table width="100%" style="font-size: small;">
			<tr>
				<td align="center" class="style4">
					危险源
				</td>
				<td align="center" class="style5">
					<input runat="server" id="tbArgs" style="width: 164px;" />
				</td>
				<td align="center" class="style6">
					事故发生时间自
				</td>
				<td class="style7" align="center">
					<input id="tbS" type="text" runat="server" style="width: 111px; height: 19px;" />
				</td>
				<td align="center" class="style8">
					至
				</td>
				<td class="style9" align="center">
					<input id="tbE" type="text" runat="server" style="width: 102px; height: 19px;" />
				</td>
				<td align="left">
					<asp:Button ID="btnSearch" runat="server" Text="搜索" OnClick="btnSearch_Click" Style="margin-left: 0px"
						class="ui-button ui-state-default ui-corner-all" />
				</td>
			</tr>
		</table>
		<div>
			<table width="100%" cellpadding="0" cellspacing="0" bordercolordark="#ffffff" border="0">
				<tr>
					<td align="left" width="100%">
						<table id="table4" style="height: auto;" cellspacing="1" cellpadding="0" width="100%"
							border="0">
							<tr>
								<td valign="top" width="100%" align="left">
									<radG:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
										GridLines="None" OnPageIndexChanged="RadGrid1_PageIndexChanged" Skin="Windows"
										Width="100%">
										<MasterTableView DataKeyNames="ID">
											<RowIndicatorColumn Visible="False">
												<HeaderStyle Width="20px" HorizontalAlign="Center"></HeaderStyle>
											</RowIndicatorColumn>
											<ExpandCollapseColumn Visible="False" Resizable="False">
												<HeaderStyle Width="20px"></HeaderStyle>
											</ExpandCollapseColumn>
											<Columns>
												<%--<radG:GridTemplateColumn HeaderText="编辑" HeaderStyle-HorizontalAlign="Center">
													<ItemStyle Width="5%" HorizontalAlign="Center" />
													<ItemTemplate>
														<asp:LinkButton ID="LinkButton1" runat="server" Text="编辑" CommandArgument='<%#Eval("ID")%>'
															CausesValidation="False" OnCommand="LinkButton1_Command"></asp:LinkButton>
													</ItemTemplate>
												</radG:GridTemplateColumn>--%>
												<radG:GridTemplateColumn HeaderText="编辑" HeaderStyle-HorizontalAlign="Center">
													<ItemStyle Width="5%" HorizontalAlign="Center" />
													<ItemTemplate>
														<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#"aqsw_aqsg_item.aspx?id="+Eval("id")

+"&update=yes&oldpage="+Server.UrlEncode(Request.Url.OriginalString) %>' ImageUrl="~/image/Edit.gif" ToolTip="编辑

改此条目"></asp:HyperLink></ItemTemplate>
												</radG:GridTemplateColumn>
												<radG:GridTemplateColumn HeaderText="编号" HeaderStyle-HorizontalAlign="Center">
													<ItemTemplate>
														<%#Container.DataSetIndex+1 %>
													</ItemTemplate>
													<ItemStyle Width="3%" HorizontalAlign="Center" />
												</radG:GridTemplateColumn>
												<%--	<radG:GridHyperLinkColumn HeaderText="危险源" DataNavigateUrlField="ID" DataNavigateUrlFormatString="aqsw_aqsg_item.aspx?id2={0}"
													DataTextField="危险源" Target="_self" HeaderStyle-HorizontalAlign="Center">
													<ItemStyle Width="10%" HorizontalAlign="Center" />
												</radG:GridHyperLinkColumn>--%>
												<radG:GridTemplateColumn AllowFiltering="False" HeaderText="危险源" UniqueName="TemplateColumn"
													ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" DataField="危险源">
													<ItemTemplate>
														<a href='<%# Eval("ID", "aqsw_aqsg_item.aspx?id={0}&oldpage="+Server.UrlEncode

(Request.Url.OriginalString)) %>'>
															<%#Eval("危险源")%>
														</a>
													</ItemTemplate>
													<ItemStyle HorizontalAlign="Center" Width="10%"></ItemStyle>
												</radG:GridTemplateColumn>
												<radG:GridBoundColumn HeaderText="事故类型" DataField="事故类型" HeaderStyle-HorizontalAlign="Center"
													AllowSorting="false">
													<ItemStyle Width="10%" HorizontalAlign="Center" />
												</radG:GridBoundColumn>
												<radG:GridHyperLinkColumn HeaderText="事故描述" DataTextField="事故描述" HeaderStyle-HorizontalAlign="Center"
													Visible="false">
													<ItemStyle Width="10%" HorizontalAlign="Center" />
												</radG:GridHyperLinkColumn>
												<radG:GridBoundColumn HeaderText="发生时间" DataField="发生时间" DataFormatString="{0:yyyy-MM-dd}"
													AllowSorting="false" HeaderStyle-HorizontalAlign="Center">
													<ItemStyle Width="10%" HorizontalAlign="Center" />
												</radG:GridBoundColumn>
												<radG:GridHyperLinkColumn HeaderText="事故处理人" DataTextField="事件处理人" HeaderStyle-HorizontalAlign="Center">
													<ItemStyle Width="10%" HorizontalAlign="Center" />
												</radG:GridHyperLinkColumn>
												<radG:GridTemplateColumn HeaderText="删除" HeaderStyle-HorizontalAlign="Center">
													<ItemStyle Width="5%" HorizontalAlign="Center" />
													<ItemTemplate>
														<asp:ImageButton CausesValidation="False" ID="Imagebutton2" runat="server" ImageUrl="~/image/delete2.gif"
															OnClientClick="javascript:return confirm('确认要删除吗?')" AlternateText="删除此条目" CommandArgument='<%#Eval("ID") %>'
															OnCommand="ImageButton2_Command"></asp:ImageButton>
													</ItemTemplate>
												</radG:GridTemplateColumn>
											</Columns>
											<NoRecordsTemplate>
												<div style="color: #F00; height: 40px; line-height: 40px;">
													<span style="margin-left: 10px; font-size: 14px; font-weight: bold;">没有符合的记录!</span>
												</div>
											</NoRecordsTemplate>
										</MasterTableView>
										<ExportSettings>
											<Pdf PageWidth="8.5in" PageHeight="11in" PageTopMargin="" PageBottomMargin="" PageLeftMargin=""
												PageRightMargin="" PageHeaderMargin="" PageFooterMargin=""></Pdf>
										</ExportSettings>
										<PagerStyle Mode="NextPrevAndNumeric" NextPagesToolTip="下一页" NextPageText="下一页" NextPageToolTip="下一页"
											PagerTextFormat="分页 : {4}  <input type='text' name='txtNum' id='txtNum' value='1' style='width:20px'/><input type='button' id='btnConfirm' value='确定'  onclick='splitPageByHand();'/>"
											PrevPagesToolTip="上一页" PrevPageText="上一页" PrevPageToolTip="上一页"></PagerStyle>
									</radG:RadGrid>
									<asp:HiddenField ID="hf" runat="server" />
									<asp:Button ID="btnHidden" runat="server" Style="display: none;" OnClick="btnHidden_Click" />
									<asp:LinkButton ID="cmdAdd" runat="server" OnClick="cmdAdd_Click">
				                    <font size="2"><img src="../image/add2.gif" border="0" alt="添加新信息"/> 添加...</font>
									</asp:LinkButton>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</div>
		<div style="text-align: right; width: 98%">
		</div>
	</div>
	</form>
</body>
</html>
<!--通用手动分页代码-->
<script type="text/javascript">

	//手动填写页码进行分页跳转
	function splitPageByHand() {
		if (!isNaN(document.getElementById("txtNum").value) ) {
			if (document.getElementById("txtNum").value > parseInt('<%=RadGrid1.PageCount%>') || document.getElementById("txtNum").value <= 0) {
				alert('输入数字不在范围内!');
				document.getElementById("txtNum").value = "1";
			}
			else {
				document.getElementById("hf").value = document.getElementById("txtNum").value;
				document.getElementById("btnHidden").click();
			}
		}
		else {
			alert('请输入数字!');
			document.getElementById("txtNum").value = "1";
		}

	}


	//为input控件重新赋值
	if(document.getElementById("txtNum")!=null)
	{
	document.getElementById("txtNum").value = '<%=RadGrid1.CurrentPageIndex+1 %>';
	}
</script>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using USTC;
using System.Data;
using System.Text;

public partial class AQJDGL_aqsw_aqsg_list : System.Web.UI.Page
{
	DM dm = new DM();
	DMOA oadm = new DMOA();

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			/************增加权限判断**********/
			controlPermission();
			/********************************/

			
			tbS.Value = DateTime.Now.ToString("yyyy-MM-dd");
			tbE.Value = DateTime.Now.ToString("yyyy-MM-dd");

			VSDataTable1 = new DataSet();
			GridComm com = new GridComm(this.RadGrid1);
			com.ModifyStyles();
			RadGrid1.PageSize = 10;
			BindRadData();
			AddDateControl();
		}
	}


	//绑定数据
	public void BindRadData()
	{
		if (Session["depId"].ToString() == "24")
		{
			//处部可以看见全部信息
			string strSQL = "select * from 安全事故  order by 发生时间 desc";
			DataSet ds = dm.getsql(strSQL);
			this.RadGrid1.DataSource = ds;
			this.RadGrid1.DataBind();
			ViewState["ajbb"] = ds;
		}
		else
		{
			string strSQL = "select * from 安全事故 where 信息归属='" + Session["depId"] + "' order by 发生时间 desc";
			DataSet ds = dm.getsql(strSQL);
			this.RadGrid1.DataSource = ds;
			this.RadGrid1.DataBind();
			ViewState["ajbb"] = ds;
		}
	}

	//RadGrid分页功能代码
	protected void RadGrid1_PageIndexChanged(object source, Telerik.WebControls.GridPageChangedEventArgs e)
	{
		//分页代码
		this.RadGrid1.DataSource = VSDataTable1;
		this.RadGrid1.CurrentPageIndex = e.NewPageIndex;
		this.RadGrid1.DataBind();
	}
	
	//时间控件默认显示
	private void AddDateControl()
	{

		tbS.Attributes.Add("class", "Wdate");
		tbS.Attributes.Add("onfocus", "new WdatePicker(this,'%Y-%M-%D',false)");
		tbE.Attributes.Add("class", "Wdate");
		tbE.Attributes.Add("onfocus", "new WdatePicker(this,'%Y-%M-%D',false)");
	}

	//添加事件
	protected void cmdAdd_Click(object sender, EventArgs e)
	{
		Response.Redirect("aqsw_aqsg_item.aspx");
	}
	
	//删除页面
	protected void ImageButton2_Command(object sender, CommandEventArgs e)
	{
		try
		{
			string strSQL = "delete from 安全事故 where ID=" + e.CommandArgument.ToString();
			dm.execsql(strSQL);
			ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert(\'删除成功!\'); </script>");
		}
		catch
		{
		}
		BindRadData();
	}

	//查询
	protected void btnSearch_Click(object sender, EventArgs e)
	{
		string args = this.tbArgs.Value.Trim();
		//输入条件为空时查询
		DateTime dt1 = DateTime.ParseExact(tbS.Value, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture);
		DateTime dt2 = DateTime.ParseExact(tbE.Value, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture);
		//对输入的时间先后做下简单判断
		if (dt1 > dt2)
		{
			ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert(\'起始时间不能比结束时间滞后...\'); </script>");
			return;
		}
		if (string.IsNullOrEmpty(args))
		{
			if (Session["depId"].ToString() == "24")
			{
				//处部
				//根据时间来查询
				string strSQL = "select * from 安全事故 where  发生时间 between '" + dt1 + "' and '" + dt2 + "' order by 发生时间 desc";
				DataSet ds = dm.getsql(strSQL);
				this.RadGrid1.DataSource = ds;
				this.RadGrid1.DataBind();
				ViewState["ajbb"] = ds;
			}
			else
			{
				string strSQL = "select * from 安全事故 where 信息归属='" + Session["depid"] + "' and 发生时间 between '" + dt1 + "' and '" + dt2 + "' order by 发生时间 desc";
				DataSet ds = dm.getsql(strSQL);
				this.RadGrid1.DataSource = ds;
				this.RadGrid1.DataBind();
				ViewState["ajbb"] = ds;
			}
		}
		else
		{
			if (Session["depId"].ToString() == "24")
			{
				//条件不为空时查询
				string strSQL = "select * from 安全事故 where  发生时间 between '" + dt1 + "' and '" + dt2 + "' and 危险源 like '%" + args + "%' order by 发生时间 desc";
				DataSet ds = dm.getsql(strSQL);
				this.RadGrid1.DataSource = ds;
				this.RadGrid1.DataBind();
				ViewState["ajbb"] = ds;
			}
			else
			{
				string strSQL = "select * from 安全事故 where 信息归属='" + Session["depid"] + "' and 发生时间 between '" + dt1 + "' and '" + dt2 + "' and 危险源 like '%" + args + "%' order by 发生时间 desc";
				DataSet ds = dm.getsql(strSQL);
				this.RadGrid1.DataSource = ds;
				this.RadGrid1.DataBind();
				ViewState["ajbb"] = ds;
			}
		}
	}

	/// <summary>
	/// 存放在ViewState的DataTable
	/// </summary>
	private DataSet VSDataTable1
	{
		get { return ViewState["ajbb"] as DataSet; }
		set { ViewState["ajbb"] = value; }
	}

	/// <summary>
	/// 普通的绑定资料
	/// </summary>
	private void PageDataBind()
	{
		this.RadGrid1.DataSource = VSDataTable1;
		this.RadGrid1.DataBind();
	}

	/// <summary>
	/// 绑定资料加换页
	/// </summary>
	/// <param name="getPageIndex">新页面Index</param>
	private void PageDataBind(Int32 getPageIndex)
	{
		this.RadGrid1.DataSource = VSDataTable1;
		this.RadGrid1.CurrentPageIndex = getPageIndex;
		this.RadGrid1.DataBind();
	}

	#region 验证权限 2012-06-22增加

	string addAuthority = "0";
	string modifyAuthority = "0";
	string deleteAuthority = "0";

	public void processPermission()
	{
		string pageUrl = Request.Url.LocalPath; //获取当前页面的地址
		ViewState["URL"] = pageUrl;
		int groupId = int.Parse(Session["groupId"].ToString()); //用户所在组
		PageBase pb = new PageBase();
		string[] permissions = pb.ValidatePermission(ViewState["URL"].ToString(), groupId);
		addAuthority = permissions[0];
		modifyAuthority = permissions[1];
		deleteAuthority = permissions[2];
	}

	/// <summary>
	/// 处理权限,在Page_Load函数中调用
	/// </summary>
	public void controlPermission()
	{
		//首先处理权限
		processPermission();
		//接下来判断增加按钮和修改及删除列
		if (addAuthority == "0")
		{
			this.cmdAdd.Visible = false;
		}
		else
		{
			this.cmdAdd.Visible = true;
		}

		if (modifyAuthority == "0")
		{
			RadGrid1.Columns[0].Visible = false;

		}
		else
		{
			RadGrid1.Columns[0].Visible = true;
		}

		if (deleteAuthority == "0")
		{
			RadGrid1.Columns[7].Visible = false;
		}
		else
		{
			RadGrid1.Columns[7].Visible = true;
		}
	}
	#endregion

     
    #region 分页功能扩展 2012-06-26

    public void btnHidden_Click(object sender, EventArgs e)
    {
        int currentPageIndex = int.Parse(hf.Value);
        this.RadGrid1.DataSource = VSDataTable1;
        this.RadGrid1.CurrentPageIndex = currentPageIndex-1;
        this.RadGrid1.DataBind();
    }

    #endregion


}





相关文章
|
3月前
|
人工智能 开发框架 .NET
.NET技术的强大功能:.NET技术的基础特性、在现代开发中的应用、以及它如何助力未来的软件开发。
.NET技术是软件开发领域的核心支柱,以其强大功能、灵活性及安全性广受认可。本文分三部分解析:基础特性如多语言支持、统一运行时环境;现代应用如企业级与Web开发、移动应用、云服务及游戏开发;以及未来趋势如性能优化、容器化、AI集成等,展望.NET在不断变化的技术环境中持续发展与创新。
108 4
|
10天前
.NET 4.0下实现.NET4.5的Task类相似功能组件
【10月更文挑战第29天】在.NET 4.0 环境下,可以使用 `BackgroundWorker` 类来实现类似于 .NET 4.5 中 `Task` 类的功能。`BackgroundWorker` 允许在后台执行耗时操作,同时不会阻塞用户界面线程,并支持进度报告和取消操作。尽管它有一些局限性,如复杂的事件处理模型和不灵活的任务管理方式,但在某些情况下仍能有效替代 `Task` 类。
|
23天前
|
开发框架 .NET 开发工具
.NET 9 中 LINQ 新增的功能
.NET 9 中 LINQ 新增的功能
|
1月前
|
监控 安全 API
Docker + .NET API:简化部署和扩展
Docker + .NET API:简化部署和扩展
35 0
|
1月前
|
监控 安全 API
最完美的扩展Docker + .NET API:简化部署和扩展
最完美的扩展Docker + .NET API:简化部署和扩展
49 0
|
3月前
|
开发框架 JavaScript 前端开发
提升生产力:8个.NET开源且功能强大的快速开发框架
提升生产力:8个.NET开源且功能强大的快速开发框架
|
3月前
|
Unix Linux C#
增强用户体验:2个功能强大的.NET控制台应用帮助库
增强用户体验:2个功能强大的.NET控制台应用帮助库
|
3月前
|
数据库
分享一个 .NET EF 6 扩展 Where 的方法
分享一个 .NET EF 6 扩展 Where 的方法
|
3月前
|
开发框架 .NET
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
118 0
|
3月前
|
设计模式 前端开发 数据可视化
LiveCharts2:简单灵活交互式且功能强大的.NET图表库
LiveCharts2:简单灵活交互式且功能强大的.NET图表库
61 0