I'm doing a project where I need to override most of the events on several controls. I'm creating a class library that I want to drop into projects, change the base page and be done. In this case, I wanto edit the onClick event for every button without changing
the type. Because this is sharepoint webpart development I am creating the buttons dynamically:
Button myButton = new Button();
and would like to keep that instead of having to change all references to a button as:
CustomButton myButton = new CustomButton();
I think what I am doing on the button itself is (The code is at work) this:
public class CustomButton : Button
{
public void override onClick { etc }
}
The syntax is probably off but its close.
My thanks for looking at this issue!
Cliff
http://www.beermagnet.com - Unleash your Pubtential!
Click is an event, not a overridable method that you can override by inherit from it. So, what you can do for this is, add a
Click event handler to the button and put all the code that you wish to override in that handler.
OK I get that but that will only work on a button by button basis.
The solution I have come up with, and I'm not sure if this is bad practice... is to create my own control that inherits button, then create another control that inherits my custom button. Then I strip out "using System.Web.UI.WebControls;" from my page so
that the 2nd control I created (which is called Button) is the only one it can reference.
so..
myButton inherits Button
Button inherits myButton
I can still do
Button mb = new Button();
Except that now it has my custom code and I can drop it in a project, erase the using System.Web.UI.WebControls, add using MyCustomButton. I tested just now with this and it doesn't error out. Tomorrow at work I will test out in a real application on dev
to see what happens.
Cliff
http://www.beermagnet.com - Unleash your Pubtential!
for the event handler, 1 event handler can use by many object, as long as the event arguments are the same. For an example,
OnClick event handler can use by many button object. As long all of them are button.
Any of the base class inherits "System.Web.UI.WebControls", the object will always have "System.Web.UI.WebControls" content. No matter how many layers of the inheritant. :)
So, good luck for you try.
No matter how much time you spend on coding. Wish you happy coding.
My Technical Blog
I don't think I explained well :) I do want it to retain the properites/methods of a button, I just want to wrap the events of the button and, indeed, all events on the page in a try/catch block automatically. and in doing so, not have to update dozens of
applications with the custom control. Think of it as... I'm trying to emulate the Global.asax within sharepoint webpart, because it has none. I think this will work.
Thanks for the help!
Cliff
http://www.beermagnet.com - Unleash your Pubtential!
dtflagg
Member
3 Points
26 Posts
Overriding control events
Jan 24, 2013 11:25 PM|LINK
I'm doing a project where I need to override most of the events on several controls. I'm creating a class library that I want to drop into projects, change the base page and be done. In this case, I wanto edit the onClick event for every button without changing the type. Because this is sharepoint webpart development I am creating the buttons dynamically:
and would like to keep that instead of having to change all references to a button as:
I think what I am doing on the button itself is (The code is at work) this:
public class CustomButton : Button { public void override onClick { etc } }The syntax is probably off but its close.
My thanks for looking at this issue!
http://www.beermagnet.com - Unleash your Pubtential!
CruzerB
Contributor
5399 Points
1098 Posts
Re: Overriding control events
Jan 25, 2013 01:18 AM|LINK
Hi,
Click is an event, not a overridable method that you can override by inherit from it. So, what you can do for this is, add a Click event handler to the button and put all the code that you wish to override in that handler.
E.g. button.Click += new EventHandler(OnClick);For detail, you can refer to Event Handling in C#My Technical Blog
dtflagg
Member
3 Points
26 Posts
Re: Overriding control events
Jan 25, 2013 07:18 AM|LINK
OK I get that but that will only work on a button by button basis.
The solution I have come up with, and I'm not sure if this is bad practice... is to create my own control that inherits button, then create another control that inherits my custom button. Then I strip out "using System.Web.UI.WebControls;" from my page so that the 2nd control I created (which is called Button) is the only one it can reference.
so..
myButton inherits Button
Button inherits myButton
I can still do
Except that now it has my custom code and I can drop it in a project, erase the using System.Web.UI.WebControls, add using MyCustomButton. I tested just now with this and it doesn't error out. Tomorrow at work I will test out in a real application on dev to see what happens.
http://www.beermagnet.com - Unleash your Pubtential!
CruzerB
Contributor
5399 Points
1098 Posts
Re: Overriding control events
Jan 25, 2013 07:35 AM|LINK
Hi,
for the event handler, 1 event handler can use by many object, as long as the event arguments are the same. For an example, OnClick event handler can use by many button object. As long all of them are button.
Any of the base class inherits "System.Web.UI.WebControls", the object will always have "System.Web.UI.WebControls" content. No matter how many layers of the inheritant. :)
So, good luck for you try.
My Technical Blog
dtflagg
Member
3 Points
26 Posts
Re: Overriding control events
Jan 25, 2013 11:33 AM|LINK
I don't think I explained well :) I do want it to retain the properites/methods of a button, I just want to wrap the events of the button and, indeed, all events on the page in a try/catch block automatically. and in doing so, not have to update dozens of applications with the custom control. Think of it as... I'm trying to emulate the Global.asax within sharepoint webpart, because it has none. I think this will work.
Thanks for the help!
http://www.beermagnet.com - Unleash your Pubtential!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Overriding control events
Jan 26, 2013 11:43 PM|LINK
Hi,
It seems that your problem is solved. So you can mark yourself as an answer
Or just anything urgent, please feel free to feedback.