Raise CustomEvent From UserControl
-
Hi.. I have a UserControl named MyControl.I have placed a Button on this UserControl.I placed this UserControl on my webForm named Default.aspx.My requirement is to raise CustomEvent when i click the Button. This is the code i tried..... //in MyControl.ascx.cs public event System.EventHandler Hello; //in Button_click event this.Hello(this, new EventArgs()); //in default.aspx.cs protected MyControl MyControl1; //in page_Load this.MyControl.Hello +=new EventHandler(MyControl_Hello); //after Page_load private void MyControl_Hello(object sender, EventArgs e) { Response.Write("welcome"); } ---------------------------------------------------------------------------- i am able to print "welcome" in 1.1 version.Above code is working fine.but in 2.o its not working.i need this in 2.0.plz anyone help me.....................
-
Hi.. I have a UserControl named MyControl.I have placed a Button on this UserControl.I placed this UserControl on my webForm named Default.aspx.My requirement is to raise CustomEvent when i click the Button. This is the code i tried..... //in MyControl.ascx.cs public event System.EventHandler Hello; //in Button_click event this.Hello(this, new EventArgs()); //in default.aspx.cs protected MyControl MyControl1; //in page_Load this.MyControl.Hello +=new EventHandler(MyControl_Hello); //after Page_load private void MyControl_Hello(object sender, EventArgs e) { Response.Write("welcome"); } ---------------------------------------------------------------------------- i am able to print "welcome" in 1.1 version.Above code is working fine.but in 2.o its not working.i need this in 2.0.plz anyone help me.....................
-
Seems ok. But if you are not using "EventArgs e" you pass null instead of "new EventArgs();". and on the other hand i think it is better to initialize event handlers in OnInit instead of Page_Load.. What do you think guys?
-
what you told is exactly correct.bcoz i got error object ref is not set to an instance of an object.but i am not getting how to handle it.i thought u hav a good knowledge on this topic.plz help me...(wats the code i need to write)
//in MyControl.ascx.cs public event System.EventHandler Hello; //in Button_click event ///I suggest: if(this.Hello != null) this.Hello(this, null); //in default.aspx.cs protected MyControl MyControl1; //in page_Load XX ///override OnInit protected override void OnInit(EventArgs e) { base.OnInit(e); this.MyControl.Hello +=new EventHandler(MyControl_Hello); } //after Page_load private void MyControl_Hello(object sender, EventArgs e) { //this is not a good way put a label and assing your value to it Response.Write("welcome"); }
-
//in MyControl.ascx.cs public event System.EventHandler Hello; //in Button_click event ///I suggest: if(this.Hello != null) this.Hello(this, null); //in default.aspx.cs protected MyControl MyControl1; //in page_Load XX ///override OnInit protected override void OnInit(EventArgs e) { base.OnInit(e); this.MyControl.Hello +=new EventHandler(MyControl_Hello); } //after Page_load private void MyControl_Hello(object sender, EventArgs e) { //this is not a good way put a label and assing your value to it Response.Write("welcome"); }