asp.net gridview分页:第一页 下一页 1 2 3 4 上一页 最末页
程序员文章站
2024-02-22 20:05:52
效果图:
功能简介:可使用上下键选中行,选中后点击修改,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(); } } }
上一篇: C#传值方式实现不同程序窗体间通信实例
下一篇: 梦幻式下拉菜单