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

2

3

4

于是我想到

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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

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

2

3

4

5

运行一下,页面都正常。