用类的继承关系(重写父类的方法)实现简易后台代码模板
程序员文章站
2024-03-05 17:39:55
asp.net的优势就在于快速构建应用,而对于一些最基础数据的增删改以及分页事件或者样式的设定可以通过在父类中写上虚方法来供子类调用,如果子类需要在模板的基础上衍生变化或者...
asp.net的优势就在于快速构建应用,而对于一些最基础数据的增删改以及分页事件或者样式的设定可以通过在父类中写上虚方法来供子类调用,如果子类需要在模板的基础上衍生变化或者索性不要父类的方法的话,则只需要重写父类的方法即可。
实验代码如下:
首先是模板类的抽象,继承自page类;
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();
}
}
然后是一个继承此模板类的具体实例;
<%@ 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>
最后就是关键的后台代码实现,此处假设需要修改grid的每页显示数和分页样式;
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;
}
}
以上仅是提供一种思路,如有错误望各网友批评指正。
实验代码如下:
首先是模板类的抽象,继承自page类;
复制代码 代码如下:
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();
}
}
然后是一个继承此模板类的具体实例;
复制代码 代码如下:
<%@ 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>
最后就是关键的后台代码实现,此处假设需要修改grid的每页显示数和分页样式;
复制代码 代码如下:
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;
}
}
以上仅是提供一种思路,如有错误望各网友批评指正。