The advantage of ASP.net is the rapid construction of applications, while for some of the most basic data additions and deletions and paging event or style settings can be written in the parent class virtual method to call the subclass, if subclasses need to derive changes on the basis of a template or simply do not want the parent-class method, then only the method of the parent class needs to be overridden.
The experimental code is as follows:
The first is the abstraction of the template class, which inherits from the Page class;
Copy Code code as follows:
public class Template:System.Web.UI.Page
{
protected override void OnLoad (EventArgs e)
{
Base. OnLoad (e);
if (! IsPostBack)
{
Datarefresh ();
}
}
protected virtual void Datarefresh ()
{
}
protected virtual void Grid_init (object sender, EventArgs e)
{
var Grid = sender as GridView;
Grid. AllowPaging = true;
Grid. PageSize = 10;
Grid. Pagersettings.mode = Pagerbuttons.numericfirstlast;
}
protected virtual void Grid_pageindexchanging (object sender, Gridviewpageeventargs e)
{
var Grid = sender as GridView;
Grid. PageIndex = E.newpageindex;
Datarefresh ();
}
}
then a concrete instance that inherits this template class;
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Child.aspx.cs" inherits= "Webtest.child"%>
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:gridview id= "Grid" runat= "Server" oninit= "Grid_init" onpageindexchanging= "grid_pageindexchanging" ></ Asp:gridview>
</div>
</form>
</body>
Finally, the key backend code implementation, where you need to modify the grid's display number and page style;
Copy Code code as follows:
public partial class Child:template
{
protected override void Datarefresh ()
{
Grid. DataSource = new Logdao (). Getlogsbyyearmonth ("2012", "10");
Grid. DataBind ();
}
protected override void Grid_init (object sender, EventArgs e)
{
Base.grid_init (sender, E);
Grid. Pagersettings.mode = Pagerbuttons.nextpreviousfirstlast;
Grid. PageSize = 15;
}
}
The above is only to provide a way of thinking, if there is a mistake to look at the netizens criticized.