gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁

效果圖:

gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁_第1张图片

功能簡介:可使用上下鍵選中行,選中後點擊修改,textbox獲得gridview中的代碼的數據。對你有幫助的話,請記得要點擊“好文要頂”哦!!!不懂的,請留言。廢話不多說了,貼碼如下:

View Code
"server">
GridView分頁



View Code
"form1" runat="server">



"btnUpd" runat="server" TabIndex="3" Text="修改" Width="59px" Font-Size="14px" OnClick="btnUpd_Click" />
"NUM" runat="server" CssClass="hidden">
"tbValue" runat="server">
"gvData" runat="server" AutoGenerateColumns="false" Width="100%"
AllowPaging="true" PageIndex="0" PageSize="15" OnPageIndexChanging="gvData_PageIndexChanging"
OnRowCreated="gvData_RowCreated" OnRowDataBound="gvData_RowDataBound">
"首頁" NextPageText="下一頁" PreviousPageText="上一頁" LastPageText="末頁"
Mode="NumericFirstLast" />

"True" Visible="False" />
"STOCK_NO" HeaderText="代號" />
"STOCK_NAME" HeaderText="字段二" />
"FACT_NO" HeaderText="字段三" />
"ADDRESS" HeaderText="字段四" />
"MEMO_DESC" HeaderText="字段五" />
"CARRYDOWN_YM" HeaderText="字段六" />
"CLOSEDOWN_YM" HeaderText="字段七" />
"CUST_FLAG" HeaderText="字段八" />

"Left" />
"LightBlue" Wrap="true" />


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("");
return;
}
else
{
intNum = Convert.ToInt16(this.NUM.Text) - 1;
tbValue.Text = this.gvData.Rows[intNum].Cells[1].Text.ToString();
}
}
}





转载于:https://www.cnblogs.com/koeltp/archive/2012/02/21/2361143.html

你可能感兴趣的:(ui,javascript,数据库)