The advantage of Asp.net is to quickly build applications. For some of the most basic data addition, deletion, modification, and paging event or style settings, you can write a virtual method in the parent class for the subclass to call, if the subclass needs to derive a change or simply not use a method of the parent class on the basis of the template, you only need to override the method of the parent class.
The experiment code is as follows::
The first is the template class abstraction, inherited from the Page class;
Copy codeThe Code is 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 specific instance that inherits the template class;
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Child. aspx. cs" Inherits = "WebTest. Child" %>
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: GridView ID = "grid" runat = "server" OnInit = "grid_Init" OnPageIndexChanging = "grid_PageIndexChanging"> </asp: GridView>
</Div>
</Form>
</Body>
</Html>
Finally, it is the key background code implementation. Here we assume that you need to modify the number of displays per page and the paging style of the grid;
Copy codeThe Code is 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. nextpreviusfirstlast;
Grid. PageSize = 15;
}
}
The above is only a way of thinking. If there is any mistake, I hope all netizens will criticize and correct it.