zoukankan      html  css  js  c++  java
  • 利用继承解决每个 cs 文件的数据绑定问题

        最近开始一个新的项目,几乎每个页面的 page_load 事件里都要写

    Page_Load
    1protected void Page_Load(object sender, EventArgs e) {
    2            if (!this.IsPostBack)
    3                Bind();
    4        }

    于是我想到

    PageBase
     1    public class PageBase : System.Web.UI.Page
     2    {
     3        protected void Page_Load(object sender, EventArgs e) {
     4
     5            if (!this.IsPostBack)
     6                Bind();
     7        }

     8
     9        protected void Page_Error(object sender, EventArgs e) {
    10            Exception ex = Server.GetLastError();
    11            //异常处理 
    12            Server.ClearError();
    13        }

    14
    15        protected virtual void Bind() { }
    16    }

    17


        然后在每个页面里继承自该 PageBase , 删掉 vs 自动生成的 Page_Load(object sender, EventArgs e) , 再重写 Bind() 方法:

    Bind()
    1        protected override void Bind() {
    2            TextBox1.Text = "测试用例";
    3            throw new Exception("用例错误!");
    4        }

    5


        运行一下,页面都正常。



  • 相关阅读:
    Eclipse详细设置护眼背景色和字体颜色并导出
    详解equals()方法和hashCode()方法
    MFC+WinPcap编写一个嗅探器之七(协议)
    fastjson使用
    2017-04-07 开通博客
    开启mysql慢查询
    MYSQL介绍安装及一些问题解决
    python基础入门
    Linux重启与关机命令
    Scanner类与Random类
  • 原文地址:https://www.cnblogs.com/infozero/p/1612119.html
Copyright © 2011-2022 走看看