当前位置: 首页 > 编程笔记 >

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

柴茂材
2023-03-14
本文向大家介绍asp.net gridview分页:第一页 下一页 1 2 3 4 上一页 最末页,包括了asp.net gridview分页:第一页 下一页 1 2 3 4 上一页 最末页的使用技巧和注意事项,需要的朋友参考一下

效果图:

功能简介:可使用上下键选中行,选中后点击修改,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();
  }
 }
}
 类似资料:
  • 我正在使用PHP实现发电机db分页。 如果想从第1页到第2页分页,一切都很好,但是如果我想从第3页到第1页,我哪儿也去不了。有人能帮我吗?

  • 我正在用ApachePDFBox编写一个简单的Java应用程序。我有几个PDF,其中最后一页是前几页内容的索引。 我需要的索引(最后一页)成为PDF文件的第一页。 可能吗? 我还发现了http://itextpdf.com/这个库听起来比ApachePDFBox更好,但在这种情况下,我也不知道我是否能做我需要的事情 或者我可以用这个:http://saaspose.com/docs/display

  • 我的分页有点问题。我有两个包含数据库数据的表,我用laravel Paginator对其进行了分页。 现在我的问题是,当你进入,例如,第2页,它添加了什么?page=2,但这使得第一个表也转到第2页。 有没有办法得到这样的东西?

  • 这件事让我毛骨悚然。我正在使用CodeIgniters分页库,现在它总是作为当前页面粘贴在第1页上。我已经检查了一大堆StackOverflow问题,我没有和其他人一样的问题。 这是我的url结构 这是我控制器里的分页代码 当我在视图中呼应分页时,它看起来像是工作的。每个链接上的网址都是正确的,一切看起来都很好。最后一个链接显示最后一个页面url,当前页面为1。然而,当我点击第2页或分页中的任何其

  • 问题内容: 如何使用AJAX获取下一页/上一页。 如何在浏览器中调用: 要么 返回的只是文本。 应该以这种格式加载页面: 1-1或1 当用户单击下一页/上一页按钮时,如何将该页码传递给ajax调用并显示结果。 另外,我该如何跟踪用户当前正在浏览的页面?以及我该如何为页面设置最大最小值,例如我有100个页面却没有拨打101页面 http://jsfiddle.net/2b8gR/5/ 的HTML J

  • 我正在创建这个WordPress主题,所有事情都进展顺利,除了一件事。我想为我的下一个/上一个页面链接设置样式,使下一个页面链接在右侧,而上一个页面链接在左侧。这是wordpress自动生成的链接,每当帖子数量达到最大值时,它就会添加到页面底部。我使用函数,我的html代码如下所示: 当然,我可以将float添加到 标记的样式中,但两个链接都将向右或向左浮动。我希望它们都在左右两边,我真的不希望在