在Asp.net业务系统的开发过程中,为了保证页面风格的一致性以及减少重复代码的编写,我们需要引入基类页的概念,即:定义一个基类页,让所有的页面都继承这个基类,并在该基类页中加入公用的属性和方法。
实际使用时,按照功能页面划分,可以定义多个基类页,如:
class FormBase class BizFormBase :FormBase class ViewFormBase : BizFormBase class EditFormBase : BizFormBase class QueryFormBase : FormBase |
QueryPeopleForm OnQueryButtonClick() { string peopleName = txtPeopleName.Text; string peopleAge = txtPeopleAge.Text; string sql; sql = string.Format(“Name Like‘%{0}%’and Age = {1}, PeopleName, peopleAge); PeopleManager manager = new PeopleManager(); this.gridMain.DataSource = manager.GetDataTable(sql); this.gridMain.DataBind(); } |
QueryPeopleForm : QueryFormBase OnQueryButtonClick() { string peopleName = txtPeopleName.Text; string peopleAge = txtPeopleAge.Text; sql = string.Format(“Name Like‘%{0}%’and Age = {1}, PeopleName, peopleAge); QueryAndBind(new PeopleManager(), sql); } |
public enum QueryOperator { //等于比较。 Equal = 0, // 不等于 NotEqual = 1, // Like比较 Like = 6 } class Query { void Add(string fieldName, string value, QueryOperator oper); string GetSql(); } QueryPeopleForm : QueryFormBase OnQueryButtonClick() { Query query = new Query(); query.Add(“Name”, txtPeopleName.Text, QueryOperator.Like); query.Add(“Age”, txtPeopleAge.Text, QueryOperator.Equal); QueryAndBind(new PeopleManager(), query.GetSql()); } |
QueryPeopleForm : QueryFormBase OnQueryButtonClick() { InitControls(gridMain); Query query = new Query(); query.Add(“Name”, txtPeopleName.Text, QueryOperator.Like); query.Add(“Age”, txtPeopleAge.Text, QueryOperator.Equal); QueryAndBind(new PeopleManager(), query.GetSql()); } |
QueryPeopleForm : QueryFormBase void Initialize() { // 指定页面对应的Manager Manager = new PeopleManager(); // 绑定控件 InitControls(gridMain, btnQuery); } void GetQueryInfo(Query query) { //获得查询条件 query.Add(“Name”, txtPeopleName.Text, QueryOperator.Like); query.Add(“Age”, txtPeopleAge.Text, QueryOperator.Equal); } QueryFormBase: private IManager manager = null; public IManager Manager { get { return manager; } set { manager = value; } } void InitControls(GridView grid, Button queryButton) { this.defaultGrid = grid; this.queryButton = queryButton; queryButton.Click += new EventHandler(QueryButton_Click); } void QueryButton_Click(object sender, EventArgs e) { Query query = new Query(); GetQueryInfo(Query); QueryAndBind(manager, query.GetSql()); } |
新闻热点
疑难解答
图片精选