locked
Overriding control events RRS feed

  • Question

  • User1830091657 posted

    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!



    Thursday, January 24, 2013 7:25 PM

Answers

  • User1830091657 posted

    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!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 25, 2013 7:33 AM

All replies

  • User614805505 posted

    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#
    Thursday, January 24, 2013 9:18 PM
  • User1830091657 posted

    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.



    Friday, January 25, 2013 3:18 AM
  • User614805505 posted

    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.

    Friday, January 25, 2013 3:35 AM
  • User1830091657 posted

    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!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 25, 2013 7:33 AM
  • User3866881 posted

    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.

    Saturday, January 26, 2013 7:43 PM