asp.net gridview分頁(yè):第一頁(yè) 下一頁(yè) 1 2 3 4 上一頁(yè) 最末頁(yè)
效果圖:
功能簡(jiǎn)介:可使用上下鍵選中行,選中后點(diǎn)擊修改,textbox獲得gridview中的代碼的數(shù)據(jù)。對(duì)你有幫助的話,請(qǐng)記得要點(diǎn)擊“好文要頂”哦!??!不懂的,請(qǐng)留言。廢話不多說(shuō)了,貼碼如下:
<head runat="server"> <title>GridView分頁(yè)</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;//請(qǐng)?zhí)砑右韵旅臻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;//定義變量 ,查詢 Grid設(shè)定樣式有用到 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 = "第一頁(yè) "; Button_IndexFirst.CommandName = "first"; Button_IndexFirst.ForeColor = Color.Blue; Button_IndexFirst.Click += new EventHandler(PageButtonClick); Button_IndexNext.Text = " 下一頁(yè) "; Button_IndexNext.CommandName = "next"; Button_IndexNext.ForeColor = Color.Blue; Button_IndexNext.Click += new EventHandler(PageButtonClick); Button_IndexPrevious.Text = "前一頁(yè) "; Button_IndexPrevious.CommandName = "previous"; Button_IndexPrevious.ForeColor = Color.Blue; Button_IndexPrevious.Click += new EventHandler(PageButtonClick); Button_IndexLast.Text = "最末頁(yè) "; 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) { //設(shè)置懸浮鼠標(biāo)指針形狀為"小手" 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('請(qǐng)先查詢並選擇一筆資料!')</script>"); return; } else { intNum = Convert.ToInt16(this.NUM.Text) - 1; tbValue.Text = this.gvData.Rows[intNum].Cells[1].Text.ToString(); } } }
- asp.net中的GridView分頁(yè)問(wèn)題
- Android入門之ActivityGroup+GridView實(shí)現(xiàn)Tab分頁(yè)標(biāo)簽的方法
- GridView分頁(yè)的實(shí)現(xiàn)以及自定義分頁(yè)樣式功能實(shí)例
- Android中實(shí)現(xiàn)多行、水平滾動(dòng)的分頁(yè)的Gridview實(shí)例源碼
- Asp.net GridView使用大全(分頁(yè)實(shí)現(xiàn))
- GridView分頁(yè)的實(shí)現(xiàn)(通用分頁(yè)模板)
- AspNetPager+GridView實(shí)現(xiàn)分頁(yè)的實(shí)例代碼
- asp.net中g(shù)ridview的查詢、分頁(yè)、編輯更新、刪除的實(shí)例代碼
- GridView高效分頁(yè)和搜索功能的實(shí)現(xiàn)代碼
- 解析GridView自帶分頁(yè)及與DropDownList結(jié)合使用
相關(guān)文章
asp.net中Timer無(wú)刷新定時(shí)器的實(shí)現(xiàn)方法
這篇文章主要介紹了asp.net中Timer無(wú)刷新定時(shí)器的實(shí)現(xiàn)方法,是一個(gè)非常具有實(shí)用價(jià)值的技巧,需要用到Ajax技術(shù),需要的朋友可以參考下2014-08-08Asp.net中通過(guò)Button打開另一個(gè)的frm
本文通過(guò)實(shí)例代碼給大家介紹了asp.net中通過(guò)button打開另一個(gè)frm的方法,非常不錯(cuò),需要的朋友參考下吧2016-12-12Visual Studio尋找C#程序必要的運(yùn)行庫(kù)文件
這篇文章主要為大家詳細(xì)介紹了Visual Studio尋找C#程序必要的運(yùn)行庫(kù)文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05asp.net?core?MVC?全局過(guò)濾器之ExceptionFilter過(guò)濾器(1)
這篇文章主要為大家詳細(xì)介紹了asp.net?core?MVC?全局過(guò)濾器之ExceptionFilter過(guò)濾器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08asp.net“服務(wù)器應(yīng)用程序不可用” 解決方法
服務(wù)器應(yīng)用程序不可用 您試圖在此 Web 服務(wù)器上訪問(wèn)的 Web 應(yīng)用程序當(dāng)前不可用。請(qǐng)點(diǎn)擊 Web 瀏覽器中的“刷新”按鈕重試您的請(qǐng)求。 管理員注意事項(xiàng): 詳述此特定請(qǐng)求失敗原因的錯(cuò)誤消息可在 Web 服務(wù)器的系統(tǒng)事件日志中找到。請(qǐng)檢查此日志項(xiàng)以查明導(dǎo)致該錯(cuò)誤發(fā)生的原因。2008-10-10淺談.NET中加密和解密的實(shí)現(xiàn)方法分享
這篇文章介紹了.NET中加密和解密的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-11-11ASP.NET Core WebApi中使用FluentValidation驗(yàn)證數(shù)據(jù)模型的方法
這篇文章主要介紹了ASP.NET Core WebApi中使用FluentValidation驗(yàn)證數(shù)據(jù)模型的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01