Add home, tail and status function to DataGrid's own panements

xiaoxiao2021-03-06  60

Add home, tail and status function to DataGrid's own panements

Zheng Zuo

2004-9-20

DataGrid provides paging features, but it seems to be limited, but we can get status through some attributes of DataGrid and add the home page, and the final page function button. There is no custom paging feature of DataGrid. If the speed efficiency is not very particular, it is good to manage the paging by DataGrid himself, and the cost of paying is to take the entire relevant data and then delete the data of the specified page. The advantage is that the development speed is fast and does not need to write a paging store. This article uses the Northwind database in SQL Server. The run interface is as follows:

For the front desk display interface, I put a DataGrid; four LinkButton-oriented buttons; four literals displays a record state.

The rest is to locate the table.

Here you need to set the instalowpaging property of the DataGrid as true, and set the allowcustomPaging property bit False (default to false), set the PagersTyle's Visible property to false, making the front desk not displayed.

The code of the front desk is as follows:

<% @ Page language = "c #" codebehind = "DataGridpaging.aspx.cs" autoeventwireup = "false" inherits = "zz.aspnetpaging.DataGridpaging"%>%>%>

DataGridPaging </ Title></p> <p><meta content = "Microsoft Visual Studio .NET 7.1" Name = "Generator"></p> <p><meta content = "c #" Name = "code_language"></p> <p><meta content = "javascript" name = "vs_defaultclientscript"></p> <p><meta content = "http://schemas.microsoft.com/intellisense/ie5" Name = "vs_targetschema"></p> <p></ HEAD></p> <p><body></p> <p><form id = "form1" method = "post" runat = "server"></p> <p><Table ID = "Table1" style = "font-size: 9pt" cellspacing = "1" cellpadding = "1" width = "450" ​​align = "center"</p> <p>Border = "1"></p> <p><Tr></p> <p><TD> <ask: DataGrid ID = "DataGrid1" runat = "server" Pagesize = "5" width = "100%" allowpaging = "true"> <headersty font-size = "9pt"> </ headerstyle></p> <p><FooterStyle Font-size = "9pt"> </ footerstyle></p> <p><PagersTyle visible = "false" font-size = "9pt" mode = "numericpages"> </ pageerstyle></p> <p></ asp: datagrid> </ td></p> <p></ TR></p> <p></ TABLE></p> <p><Table ID = "Table2" style = "font-size: 9pt" cellspacing = "1" cellpadding = "1" width = "450" ​​align = "center"</p> <p>Border = "1"></p> <p><Tr></p> <p><TD Style = "Width: 207px"></p> <p><ask: linkbutton id = "lbtnfirst" runat = "server" commandname = "first"> Home </ asp: linkbutton></p> <p><asp: linkbutton id = "lbtnprev" runat = "server" CommandName = "prev"> Previous </ asp: linkbutton></p> <p><ask: linkbutton id = "lbtnnext" runat = "server" CommandName = "Next"> Next Page </ asp: linkbutton></p> <p><ask: linkbutton id = "lbtnlast" runat = "server" CommandName = "last"> Last "</ asp: linkbutton> </ td></p> <p><TD></p> <p><asp: literal id = "ltlpageindex" runat = "server"> </ asp: literal> page</p> <p><asp: literal id = "ltlpagecount" runat = "server"> </ asp: literal> page per page</p> <p><asp: literal id = "ltlpagesize" runat = "server"> </ asp: literal></p> <p><asp: literal id = "ltlrecordcount" runat = "server"> </ asp: literal></p> <p></ Td></p> <p></ TR></p> <p></ Table> </ form></p> <p></ body></p> <p></ Html></p> <p>Backstage CS file code, DataGridPaging class inherits from system.web.ui.page, pay attention to no data when data binding (0 pages), and avoiding the front desk in the back page when the number of pages is reduced.</p> <p>Using system;</p> <p>Using system.collections;</p> <p>Using system.componentmodel;</p> <p>Using system.data;</p> <p>Using system.drawing;</p> <p>Using system.Web;</p> <p>Using system.Web.SessionState;</p> <p>Using system.Web.ui;</p> <p>Using system.Web.ui.webcontrols;</p> <p>Using system.Web.ui.htmlcontrols;</p> <p>Using system.data.sqlclient;</p> <p>Using system.configuration;</p> <p>Namespace zz.aspnetPaging</p> <p>{</p> <p>Public Class DataGridPaging: System.Web.ui.page</p> <p>{</p> <p>Private static string connString = configurationSettings.appsettings ["connString"];</p> <p>PRIVATE INT RecordCount;</p> <p>PRIVATE INT pageCOUNT;</p> <p>Protected system.Web.ui.WebControls.LinkButton lbtnfirst;</p> <p>Protected system.Web.ui.WebControls.LinkButton lbtnprev;</p> <p>Protected system.web.ui.webcontrols.linkButton lbtnnext;</p> <p>Protected system.Web.ui.WebControls.LinkButton lbtnlast;</p> <p>protected system.web.ui.webcontrols.literal ltlpageindex;</p> <p>Protected system.web.ui.webcontrols.literal ltlpagecount;</p> <p>Protected system.web.ui.webcontrols.literal ltlpagesize;</p> <p>Protected system.Web.ui.WebControls.litral ltlrecordcount;</p> <p>Protected system.web.ui.webcontrols.dataGrid DataGrid1;</p> <p>Private Void Page_Load (Object Sender, System.EventArgs E)</p> <p>{</p> <p>IF (! page.ispostback)</p> <p>{</p> <p>DataGriddatabind ();</p> <p>}</p> <p>}</p> <p>// Binding data</p> <p>Private void DataGridDataBind ()</p> <p>{</p> <p>DataSet DS = getCustomersData ();</p> <p>Recordcount = ds.tables [0] .rows.count;</p> <p>/ / Get the current number of pages</p> <p>PageCount = (int) Math.ceiling (RecordCount * 1.0 / Pagesize);</p> <p>// Avoid record from time to time, and has been reversed, CURRENTPAGEINDEX> PAGECOUNT error</p> <p>IF (RecordCount == 0)</p> <p>{</p> <p>this.dataGrid1.currentpageIndex = 0;</p> <p>}</p> <p>Else if (this.DataGrid1.currentpageindex> = PageCount) {</p> <p>THIS.DATAGRID1.CURRENTPAGEINDEX = PAGECUNT - 1;</p> <p>}</p> <p>THIS.DATAGRID1.DATASOURCE = DS;</p> <p>THIS.DATAGRID1.DATABIND ();</p> <p>NavigationStateChange ();</p> <p>}</p> <p>#Region web form designer generated code</p> <p>Override protected void oninit (Eventargs E)</p> <p>{</p> <p>//</p> <p>// Codegen: This call is necessary for the ASP.NET Web Form Designer.</p> <p>//</p> <p>InitializationComponent ();</p> <p>Base.onit (E);</p> <p>}</p> <p>/// <summary></p> <p>/// Designer supports the required method - do not use the code editor to modify</p> <p>/// This method is content.</p> <p>/// </ summary></p> <p>Private vidinitiRizeComponent ()</p> <p>{</p> <p>THIS.LBTNFIRST.Click = new system.eventhandler (this.lbtnavigation_click);</p> <p>THIS.LBTNPREV.Click = new system.EventHandler (this.lbtnavigation_click);</p> <p>THIS.LBTNNEXT.Click = new system.eventhandler (this.lbtnnavigation_click);</p> <p>THIS.LBTNLAST.CLICK = New System.EventHandler (this.lbtnnaVigation_click);</p> <p>This.Load = New System.EventHandler (this.page_load);</p> <p>}</p> <p>#ndregion</p> <p>Private void lbtnnavigation_click (Object Sender, System.Eventargs E)</p> <p>{</p> <p>LinkButton BTN = (LinkButton) Sender;</p> <p>Switch (btn.commandname)</p> <p>{</p> <p>Case "first":</p> <p>PageIndex = 0;</p> <p>Break;</p> <p>CASE "prev": // if (pageindex> 0)</p> <p>PageIndex = PageIndex - 1;</p> <p>Break;</p> <p>Case "next": // if (pageIndex <pagecount -1)</p> <p>PageIndex = PageIndex 1;</p> <p>Break;</p> <p>Case "Last":</p> <p>PageIndex = PageCount - 1;</p> <p>Break;</p> <p>}</p> <p>DataGriddatabind ();</p> <p>}</p> <p>// data binding</p> <p>Public Static Dataset getCustomersData ()</p> <p>{</p> <p>SqlConnection conn = new sqlconnection (connString);</p> <p>String Sqlstr = "Select Customerid, CompanyName, Address, Phone from Customers";</p> <p>SQLCommand Comm = New Sqlcommand (SQLSTR, CONN);</p> <p>SqlDataAdapter DataAdapter = New SqlDataAdapter (Comm);</p> <p>DataSet DS = New Dataset ();</p> <p>DataAptapter.Fill (DS); Return DS;</p> <p>}</p> <p>/// <summary></p> <p>// / Control the status of the navigation button or number</p> <p>/// </ summary></p> <p>Public void navigationStateChange ()</p> <p>{</p> <p>IF (PageCount <= 1) // (RecordCount <= PageSize) / / less than or equal to one page</p> <p>{</p> <p>THIS.LBTNFIRST.ENABLED = FALSE;</p> <p>this.lbtnprev.enabled = false;</p> <p>THIS.LBTNNEXT.ENABLED = FALSE;</p> <p>THIS.LBTNLAST.ENABLED = FALSE;</p> <p>}</p> <p>Else / / Multi-page</p> <p>{</p> <p>IF (pageIndex == 0) // currently the first page</p> <p>{</p> <p>THIS.LBTNFIRST.ENABLED = FALSE;</p> <p>this.lbtnprev.enabled = false;</p> <p>THIS.LBTNNEXT.ENABLED = True;</p> <p>THIS.LBTNLAST.ENABLED = True;</p> <p>}</p> <p>Else IF (PageIndex == PageCount - 1) // Currently for the last page</p> <p>{</p> <p>THIS.LBTNFIRST.ENABLED = TRUE;</p> <p>THIS.LBTNPREV.ENABLED = TRUE;</p> <p>THIS.LBTNNEXT.ENABLED = FALSE;</p> <p>THIS.LBTNLAST.ENABLED = FALSE;</p> <p>}</p> <p>ELSE // Intermediate Page</p> <p>{</p> <p>THIS.LBTNFIRST.ENABLED = TRUE;</p> <p>THIS.LBTNPREV.ENABLED = TRUE;</p> <p>THIS.LBTNNEXT.ENABLED = True;</p> <p>THIS.LBTNLAST.ENABLED = True;</p> <p>}</p> <p>}</p> <p>IF (RecordCount == 0) // DataGrid.pageCount will display 1 page when there is no record</p> <p>THIS.LTLPAGECOUNT.TEXT = "0";</p> <p>Else</p> <p>THIS.LTLPAGECOUNT.TEXT = PageCount.toString ();</p> <p>IF (RecordCount == 0)</p> <p>this.ltlpageIndex.text = "0";</p> <p>Else</p> <p>THIS.LTLPAGEINDEX.TEXT = (PageIndex 1) .tostring (); // In the case of the number of pages, the front display page number plus 1</p> <p>THIS.LTLPAGESIZE.TEXT = PAGESIZE.TOSTRING ();</p> <p>THIS.LTLRECORDCOUNT.TEXT = RecordCount.toString ();</p> <p>}</p> <p>// total pages</p> <p>Public int pageCount</p> <p>{</p> <p>Get {return this.datagrid1.pagecount;</p> <p>}</p> <p>// page size</p> <p>Public Int PageSize</p> <p>{</p> <p>Get {return this.datagrid1.pagesize;</p> <p>}</p> <p>// Page Index, starting from scratch</p> <p>Public int pageIndex</p> <p>{</p> <p>Get {return this.datagrid1.currentpageindex;}</p> <p>Set {this.dataGrid1.currentpageindex = value;</p> <p>}</p> <p>// Total number of records</p> <p>Public int Recordcount</p> <p>{</p> <p>Get {return.</p> <p>Set {recordcount = value;}</p> <p>}</p> <p>}</p> <p>}</p> <p>If you need to pursue execution efficiency, it is recommended to use DataGrid's custom paging feature if the amount of data is relatively large. The stored procedure executes only one page. If you have any good suggestions or find questions, please leave a message on Blog.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-114150.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="114150" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.032</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'ze7_2F32Qx9gWATcddTapu_2BLfCniWRY0YFpuePF8MfZaOV0s6DFke_2FNW6V4kIn7rAE7_2BqQsrjPGAnlIsV8'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>