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

 更新时间:2014年12月11日 15:50:40   投稿:mdxy-dxy  
这篇文章主要介绍了asp.net gridview分页:第一页 下一页 1 2 3 4 上一页 最末页,可使用上下键选中行,选中后点击修改,textbox获得gridview中的代码的数据,需要的朋友可以参考下

效果图:

功能简介:可使用上下键选中行,选中后点击修改,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();
  }
 }
}

相关文章

  • asp.net中Timer无刷新定时器的实现方法

    asp.net中Timer无刷新定时器的实现方法

    这篇文章主要介绍了asp.net中Timer无刷新定时器的实现方法,是一个非常具有实用价值的技巧,需要用到Ajax技术,需要的朋友可以参考下
    2014-08-08
  • Asp.net中通过Button打开另一个的frm

    Asp.net中通过Button打开另一个的frm

    本文通过实例代码给大家介绍了asp.net中通过button打开另一个frm的方法,非常不错,需要的朋友参考下吧
    2016-12-12
  • .NET日志框架Nlog使用介绍

    .NET日志框架Nlog使用介绍

    这篇文章介绍了.NET日志框架Nlog的使用方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-01-01
  • Visual Studio寻找C#程序必要的运行库文件

    Visual Studio寻找C#程序必要的运行库文件

    这篇文章主要为大家详细介绍了Visual Studio寻找C#程序必要的运行库文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • 快速入门ASP.NET Core看这篇就够了

    快速入门ASP.NET Core看这篇就够了

    ASP.NET Core 是一个由微软创建的,用于构建 web 应用、API、微服务 的 web 框架。通过本文的学习就能快速的入门ASP.NET Core,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • asp.net core MVC 全局过滤器之ExceptionFilter过滤器(1)

    asp.net core MVC 全局过滤器之ExceptionFilter过滤器(1)

    这篇文章主要为大家详细介绍了asp.net core MVC 全局过滤器之ExceptionFilter过滤器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • .net core异常中间件的使用

    .net core异常中间件的使用

    本文主要简单介绍一下异常中间件的使用,学习.net core的同学可以了解下本文
    2021-06-06
  • asp.net“服务器应用程序不可用” 解决方法

    asp.net“服务器应用程序不可用” 解决方法

    服务器应用程序不可用 您试图在此 Web 服务器上访问的 Web 应用程序当前不可用。请点击 Web 浏览器中的“刷新”按钮重试您的请求。 管理员注意事项: 详述此特定请求失败原因的错误消息可在 Web 服务器的系统事件日志中找到。请检查此日志项以查明导致该错误发生的原因。
    2008-10-10
  • 浅谈.NET中加密和解密的实现方法分享

    浅谈.NET中加密和解密的实现方法分享

    这篇文章介绍了.NET中加密和解密的实现方法,有需要的朋友可以参考一下
    2013-11-11
  • ASP.NET Core WebApi中使用FluentValidation验证数据模型的方法

    ASP.NET Core WebApi中使用FluentValidation验证数据模型的方法

    这篇文章主要介绍了ASP.NET Core WebApi中使用FluentValidation验证数据模型的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01

最新评论