华域联盟 .Net 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;//�添加以下命名空�g
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;//定�x�量 ,查� Grid�O定�邮接杏玫�
 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('�先查��K�x�褚还P�Y料!')</script>");
   return;
  }
  else
  {
   intNum = Convert.ToInt16(this.NUM.Text) - 1;
   tbValue.Text = this.gvData.Rows[intNum].Cells[1].Text.ToString();
  }
 }
}
您可能感兴趣的文章:

  • asp.net中的GridView分页问题
  • Android入门之ActivityGroup+GridView实现Tab分页标签的方法
  • GridView分页的实现以及自定义分页样式功能实例
  • Android中实现多行、水平滚动的分页的Gridview实例源码
  • Asp.net GridView使用大全(分页实现)
  • GridView分页的实现(通用分页模板)
  • AspNetPager+GridView实现分页的实例代码
  • asp.net中gridview的查询、分页、编辑更新、删除的实例代码
  • GridView高效分页和搜索功能的实现代码
  • 解析GridView自带分页及与DropDownList结合使用

本文由 华域联盟 原创撰写:华域联盟 » asp.net gridview分页:第一页 下一页 1 2 3 4 上一页 最末页

转载请保留出处和原文链接:https://www.cnhackhy.com/55911.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部