Implements simple background code Templates using class inheritance relationships, and inherits background code templates.
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;
1 public class Template : System.Web.UI.Page 2 { 3 4 protected override void OnLoad(EventArgs e) 5 { 6 base.OnLoad(e); 7 8 if (!IsPostBack) 9 {10 DataRefresh();11 }12 }13 14 protected virtual void DataRefresh()15 {16 }17 18 protected virtual void grid_Init(object sender, EventArgs e)19 {20 var grid = sender as GridView;21 grid.AllowPaging = true;22 grid.PageSize = 10;23 grid.PagerSettings.Mode = PagerButtons.NumericFirstLast;24 }25 26 protected virtual void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)27 {28 var grid = sender as GridView;29 grid.PageIndex = e.NewPageIndex;30 DataRefresh();31 }32 }
Then there is a specific instance that inherits this template class;
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Child.aspx.cs" Inherits="WebTest.Child" %> 2 3 <!DOCTYPE html> 4 5
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;
1 public partial class Child : Template 2 { 3 4 protected override void DataRefresh() 5 { 6 grid.DataSource = new LogDao().GetLogsByYearMonth("2012", "10"); 7 grid.DataBind(); 8 } 9 10 protected override void grid_Init(object sender, EventArgs e)11 {12 base.grid_Init(sender, e);13 14 grid.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast;15 grid.PageSize = 15;16 }17 18 }
The above is only a way of thinking. If there is any mistake, I hope all netizens will criticize and correct it.