asp.net gridview分页:第一页 下一页 1 2 3 4 上一页 最末页

 更新时间:2021年9月22日 10:11  点击:1497

效果图:

功能简介:可使用上下键选中行,选中后点击修改,textbox获得gridview中的代码的数据。对你有帮助的话,请记得要点击“好文要顶”哦!!!不懂的,请留言。废话不多说了,贴码如下:

<head runat="server">
 <title>GridView分頁</title>
 <script type="text/javascript">
  var currentRowId = 0;
  var styleName = "";
  function SelectRow(ev, strGvName) {
   var e = window.event || ev;
   var keyCode = -1;
   if (e.which == null)
    keyCode = e.keyCode; // IE 
   else
    if (e.which > 0)
    keyCode = e.which; // All others 
   if (keyCode == 40)
    MarkRow(currentRowId + 1, strGvName);
   if (keyCode == 38) {
    MarkRow(currentRowId - 1, strGvName);
   }

   document.getElementById("NUM").value = currentRowId;
  }
  function MarkRow(rowId, strGvName) {
   var Grid = document.getElementById(strGvName);
   var rowCount = Grid.rows.length;
   if (document.getElementById(strGvName + rowId) == null)
    return;
   if (rowId == rowCount) {
    return;
   }
   if (document.getElementById(strGvName + currentRowId) != null)
    document.getElementById(strGvName + currentRowId).style.backgroundColor = styleName;
   currentRowId = rowId;
   styleName = document.getElementById(strGvName + rowId).style.backgroundColor;
   document.getElementById(strGvName + rowId).style.backgroundColor = 'red';
   var obj = document.getElementById(strGvName);
   obj.rows[rowId].cells[0].focus();
   document.getElementById("NUM").value = currentRowId;

  }
 </script>

 <style type="text/css">
  .hidden
  {
   display: none;
  }
 </style>
</head>

核心代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;//請添加以下命名空間
using System.Data;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
 SqlConnection con = new SqlConnection("Server=SERVER\\xxx;Database=xxxx;User ID=xx;Pwd=xx;");
 private int _i = 0;//定義變量 ,查詢 Grid設定樣式有用到
 protected void Page_Load(object sender, EventArgs e)
 {
  if (!Page.IsPostBack)
  {
   getBind();
  }
 }
 protected void getBind()
 {
  string str = "select * from im01";
  DataSet ds = new DataSet();
  SqlDataAdapter da = new SqlDataAdapter(str, con);
  da.Fill(ds);
  DataTable dt = ds.Tables[0];
  gvData.DataSource = dt;
  gvData.DataBind();
 }
 protected void gvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
 {

 }
 protected void gvData_RowCreated(object sender, GridViewRowEventArgs e)
 {
  if (e.Row.RowType == DataControlRowType.Pager)
  {
   Label label_Index = new Label();
   LinkButton Button_IndexFirst = new LinkButton();
   LinkButton Button_IndexLast = new LinkButton();
   LinkButton Button_IndexNext = new LinkButton();
   LinkButton Button_IndexPrevious = new LinkButton();

   Button_IndexFirst.Text = "第一頁 ";
   Button_IndexFirst.CommandName = "first";
   Button_IndexFirst.ForeColor = Color.Blue;
   Button_IndexFirst.Click += new EventHandler(PageButtonClick);

   Button_IndexNext.Text = " 下一頁 ";
   Button_IndexNext.CommandName = "next";
   Button_IndexNext.ForeColor = Color.Blue;

   Button_IndexNext.Click += new EventHandler(PageButtonClick);

   Button_IndexPrevious.Text = "前一頁 ";
   Button_IndexPrevious.CommandName = "previous";
   Button_IndexPrevious.ForeColor = Color.Blue;
   Button_IndexPrevious.Click += new EventHandler(PageButtonClick);

   Button_IndexLast.Text = "最末頁 ";
   Button_IndexLast.CommandName = "last";
   Button_IndexLast.ForeColor = Color.Blue;
   Button_IndexLast.Click += new EventHandler(PageButtonClick);

   e.Row.Controls[0].Controls[0].Controls[0].Controls[0].Controls.AddAt(0, (Button_IndexFirst));
   e.Row.Controls[0].Controls[0].Controls[0].Controls[0].Controls.AddAt(1, (Button_IndexPrevious));

   int controlTmp = e.Row.Controls[0].Controls[0].Controls[0].Controls.Count - 1;
   e.Row.Controls[0].Controls[0].Controls[0].Controls[controlTmp].Controls.Add(Button_IndexNext);
   e.Row.Controls[0].Controls[0].Controls[0].Controls[controlTmp].Controls.Add(Button_IndexLast);
  }
 }
 protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
 {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
   //设置悬浮鼠标指针形状为"小手" 
   e.Row.Attributes["style"] = "Cursor:hand";
  }
  string strGvName = "gvData";
  e.Row.Attributes.Add("id", strGvName + _i.ToString());
  e.Row.Attributes.Add("onKeyDown", "SelectRow(event,'" + strGvName + "');");
  e.Row.Attributes.Add("onClick", "MarkRow(" + _i.ToString() + ",'" + strGvName + "');");
  e.Row.Attributes.Add("tabindex", "0");
  _i++;
 }
 protected void PageButtonClick(object sender, EventArgs e)
 {
  LinkButton clickedButton = ((LinkButton)sender);
  if (clickedButton.CommandName == "first")
  {
   gvData.PageIndex = 0;
  }
  else if (clickedButton.CommandName == "next")
  {
   if (gvData.PageIndex < gvData.PageCount - 1)
   {
    gvData.PageIndex += 1;
   }
  }
  else if (clickedButton.CommandName == "previous")
  {
   if (gvData.PageIndex >= 1)
   {
    gvData.PageIndex -= 1;
   }
  }
  else if (clickedButton.CommandName == "last")
  {
   gvData.PageIndex = gvData.PageCount - 1;
  }
  getBind();
 } 
 //修改
 protected void btnUpd_Click(object sender, EventArgs e)
 {
  int intNum = 0;
  if (this.NUM.Text == "" || this.NUM.Text == "0")
  {
   Response.Write("<script type=\"text/javascript\">alert('請先查詢並選擇一筆資料!')</script>");
   return;
  }
  else
  {
   intNum = Convert.ToInt16(this.NUM.Text) - 1;
   tbValue.Text = this.gvData.Rows[intNum].Cells[1].Text.ToString();
  }
 }
}

[!--infotagslink--]

相关文章

  • ASP.NET购物车实现过程详解

    这篇文章主要为大家详细介绍了ASP.NET购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • php KindEditor文章内分页的实例方法

    我们这里介绍php与KindEditor编辑器使用时如何利用KindEditor编辑器的分页功能实现文章内容分页,KindEditor编辑器在我们点击分页时会插入代码,我们只要以它为分切符,就...2016-11-25
  • 自己动手写的jquery分页控件(非常简单实用)

    最近接了一个项目,其中有需求要用到jquery分页控件,上网也找到了需要分页控件,各种写法各种用法,都是很复杂,最终决定自己动手写一个jquery分页控件,全当是练练手了。写的不好,还请见谅,本分页控件在chrome测试过,其他的兼容性...2015-10-30
  • 在ASP.NET 2.0中操作数据之七十二:调试存储过程

    在开发过程中,使用Visual Studio的断点调试功能可以很方便帮我们调试发现程序存在的错误,同样Visual Studio也支持对SQL Server里面的存储过程进行调试,下面就让我们看看具体的调试方法。...2021-09-22
  • jquery实现的伪分页效果代码

    本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
  • vue.js 表格分页ajax 异步加载数据

    Vue.js通过简洁的API提供高效的数据绑定和灵活的组件系统.这篇文章主要介绍了vue.js 表格分页ajax 异步加载数据的相关资料,需要的朋友可以参考下...2016-10-20
  • ASP.NET Core根据环境变量支持多个 appsettings.json配置文件

    这篇文章主要介绍了ASP.NET Core根据环境变量支持多个 appsettings.json配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
  • 记一次EFCore类型转换错误及解决方案

    这篇文章主要介绍了记一次EFCore类型转换错误及解决方案,帮助大家更好的理解和学习使用asp.net core,感兴趣的朋友可以了解下...2021-09-22
  • Springboot如何使用mybatis实现拦截SQL分页

    这篇文章主要介绍了Springboot使用mybatis实现拦截SQL分页,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-19
  • PHP 一个完整的分页类(附源码)

    在php中要实现分页比起asp中要简单很多了,我们核心就是直接获取当前页面然后判断每页多少再到数据库中利用limit就可以实现分页查询了,下面我来详细介绍分页类实现程序...2016-11-25
  • jquery实现的伪分页效果代码

    本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
  • 基于jquery实现表格无刷新分页

    这篇文章主要介绍了基于jquery实现表格无刷新分页,功能实现了前端排序功能,增加了前端搜索功能,感兴趣的小伙伴们可以参考一下...2016-01-08
  • C#实现3步手动建DataGridView的方法

    这篇文章主要介绍了C#实现3步手动建DataGridView的方法,实例分析了C#实现手动创建DataGridView的原理与技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • AngularJS实现分页显示数据库信息

    这篇文章主要为大家详细介绍了AngularJS实现分页显示数据库信息效果的相关资料,感兴趣的小伙伴们可以参考一下...2016-07-06
  • vue实现页面打印自动分页的两种方法

    这篇文章主要为大家详细介绍了vue实现页面打印自动分页的两种方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-29
  • 详解ASP.NET Core 中基于工厂的中间件激活的实现方法

    这篇文章主要介绍了ASP.NET Core 中基于工厂的中间件激活的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-22
  • C#中DataGridView动态添加行及添加列的方法

    这篇文章主要介绍了C#中DataGridView动态添加行及添加列的方法,涉及C#中DataGridView针对行与列动态操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • asp.net通过消息队列处理高并发请求(以抢小米手机为例)

    这篇文章主要介绍了asp.net通过消息队列处理高并发请求(以抢小米手机为例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
  • ASP.NET单选按钮控件RadioButton常用属性和方法介绍

    RadioButton又称单选按钮,其在工具箱中的图标为 ,单选按钮通常成组出现,用于提供两个或多个互斥选项,即在一组单选钮中只能选择一个...2021-09-22
  • C#中datagridview使用tooltip控件显示单元格内容的方法

    这篇文章主要介绍了C#中datagridview使用tooltip控件显示单元格内容的方法,实例分析了C#控件的相关使用技巧,需要的朋友可以参考下...2020-06-25