asp.net RadGrid分頁功能擴充套件Demo
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
{
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="分頁 : {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>
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
}
相關文章
- ASP.NET Core擴充套件庫之Http通用擴充套件ASP.NET套件HTTP
- 分類擴充套件套件
- 擴充套件你的KUBECTL功能套件
- SpringBoot-11 擴充套件功能Spring Boot套件
- 數論分塊擴充套件套件
- ASP.NET Core擴充套件庫之日誌ASP.NET套件
- kotlin 擴充套件(擴充套件函式和擴充套件屬性)Kotlin套件函式
- ASP.NET擴充套件庫之Http日誌ASP.NET套件HTTP
- Jmeter——元件擴充套件,使其功能更全面JMeter元件套件
- 【Kotlin】擴充套件屬性、擴充套件函式Kotlin套件函式
- [外掛擴充套件]單頁管理外掛套件
- 虛擬主機支援哪些擴充套件功能套件
- WPF之花式控制元件功能擴充套件控制元件套件
- python算術運算子的擴充套件功能Python套件
- ASP.NET Core擴充套件庫之實體對映ASP.NET套件
- 擴充套件工具套件
- Sanic 擴充套件套件
- Mybatis擴充套件MyBatis套件
- SpringMVC 擴充套件SpringMVC套件
- ORACLE 擴充套件Oracle套件
- iOS開發的分類和擴充套件iOS套件
- 中文分詞 PHP 擴充套件 SCWS 安裝中文分詞PHP套件
- [外掛擴充套件]js版本分頁外掛套件JS
- 使用Kotlin擴充套件函式擴充套件Spring Data案例Kotlin套件函式Spring
- JMeter 擴充套件開發:擴充套件 TCP 取樣器JMeter套件TCP
- ASP.NET Core擴充套件庫之Http請求模擬ASP.NET套件HTTP
- 開發教程 | 全志T507去掉IO擴充套件晶片後,如何保留擴充套件引腳功能套件晶片
- ?用Chrome擴充套件管理器, 管理你的擴充套件Chrome套件
- iOS 通知擴充套件iOS套件
- swift擴充套件ExtensionsSwift套件
- 擴充套件BSGS/exBSGS套件
- Json擴充套件方法JSON套件
- 提高擴充套件性套件
- HttpContext擴充套件類HTTPContext套件
- DOM部分擴充套件套件
- LINQ擴充套件方法套件
- Flask 自建擴充套件Flask套件
- 新增php擴充套件PHP套件
- 擴充套件表示式套件