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 }