事件委托,当此类(继承自Repeater)加载(Load事件)时,执行DBind方法!!
- C# code
public class Repeat2: Repeater { public void DBind(object sender, EventArgs e) { this.DataSource = a; this.DataBind(); }}
用+=,下面不行!!
- C# code
public class Repeat2: Repeater { public void DBind(object sender, EventArgs e) { this.Load += new EventHandler(DBind);//这样不行?? this.DataSource = a; this.DataBind(); }}
那应该怎么写呢,我不想在页面中去执行方法,让他自动绑定这个方法DBind
------解决方案--------------------------------------------------------
为什么不写在load方法中呢,页面中好像有个OnInit的方法,写在BasePage类中,然后对其继承 .可以看下页面生命周期
------解决方案--------------------------------------------------------
两种任选一个:
- C# code
public class Class1: Repeater { protected override void OnLoad(EventArgs e) { base.OnLoad(e); //todo: 增加你的代码 } public Class1() { this.Load += new EventHandler(Class1_Load); } void Class1_Load(object sender, EventArgs e) { //todo: 增加你的代码 }}
------解决方案--------------------------------------------------------
语法问题,学习.