用类的继承关系(重写父类的方法)实现简易后台代
模板类的代码如下:
```csharp
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();
}
}
```
接下来,我们有一个继承此模板类的具体实例,这是一个名为Child的页面。在这个页面中,我们使用了GridView控件,并为其绑定了grid_Init和grid_PageIndexChanging事件。
Child页面的HTML代码如下:
```html
<%@ Page Language="C" AutoEventWireup="true" CodeBehind="Child.aspx.cs" Inherits="WebTest.Child" %>