473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Overriding (?) a function in a control.

I'm going to create a server control in ASP.Net/C# that reacts to a couple
of buttons. Thing is - I want to have the functions there for the buttons so
if somebody doesn't program the command for it, it will still work - just
not do anything. The norm will be that there will be code for the button
though.

How do I go about doing this? Is this overloading?

TIA - Jeff.
Sep 14 '07 #1
4 1281
"Mufasa" <jb@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
I'm going to create a server control in ASP.Net/C# that reacts to a couple
of buttons. Thing is - I want to have the functions there for the buttons
so if somebody doesn't program the command for it, it will still work -
just not do anything. The norm will be that there will be code for the
button though.

How do I go about doing this? Is this overloading?
You could fire an event when either button is clicked. If the event is
not hooked by the client code, you will know in your server control because
the event delegate contains null, so you can provide a default behavior when
this is the case (or simply not do anything if that is what you want by
default).
Sep 14 '07 #2
Thanks for the info. Could you point somewhere with an example. I guess I
understand the concept but I don't know how to implement it.

TIA - Jeff.

"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:et**************@TK2MSFTNGP04.phx.gbl...
"Mufasa" <jb@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I'm going to create a server control in ASP.Net/C# that reacts to a
couple of buttons. Thing is - I want to have the functions there for the
buttons so if somebody doesn't program the command for it, it will still
work - just not do anything. The norm will be that there will be code for
the button though.

How do I go about doing this? Is this overloading?

You could fire an event when either button is clicked. If the event is
not hooked by the client code, you will know in your server control
because the event delegate contains null, so you can provide a default
behavior when this is the case (or simply not do anything if that is what
you want by default).


Sep 14 '07 #3
"Mufasa" <jb@nowhere.comwrote in message
news:eI**************@TK2MSFTNGP02.phx.gbl...
Thanks for the info. Could you point somewhere with an example. I guess I
understand the concept but I don't know how to implement it.
For example, imagine that your server control has some code like this to
implement the event:

public delegate void MyButtonClickedEventHandler(object sender,
MyButtonClickedEventArgs e);
public event MyButtonClickedEventHandler MyButtonClicked;

void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
if (this.MyButtonClicked != null)
this.MyButtonClicked(this, new
MyButtonClickedEventArgs(eventArgument));
}
Observe the place where a check is done to see if the client has connected
to the event: if (this.MyButtonClicked != null)...

Here you can add an "else" and provide default behavior in case the client
has not connected a routine to the event. Otherwise, if the user wants to
add some code to process the click of the button, they just need to create
the event routine and connect it to the MyButtonClicked event of your
control, just like you would connect to any other event in any other
control.

Sep 14 '07 #4
Small comment...

Alberto Poblacion wrote:
[...]
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
if (this.MyButtonClicked != null)
this.MyButtonClicked(this, new
MyButtonClickedEventArgs(eventArgument));
}
IMHO, the above should read more like this:

void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
MyButtonClickedEventHandler handler =
this.MyButtonClicked;

if (handler != null)
{
handler(this, new MyButtonClickedEventArgs(eventArgument));
}
else
{
// implement default behavior here
}
}

This will ensure that one thread unsubscribing the event at the same
time another thread raises it won't result in a null exception.

And a minor clarification just to make the intent of this approach
clear: what Alberto is saying is that client code wouldn't subscribe to
the button click event at all; instead, the client code would subscribe
to this new event, and the control would respond to the button click by
raising this new event.

That way, the control has the opportunity to observe whether anyone is
actually subscribed to the event, allowing it to provide default
behavior if no other code is.

Now, all that said, I don't know what the implications are in ASP.NET,
but in a regular Forms application I would first approach this by
overriding the OnClick method in the control itself. There you could
replicate the event raising behavior yourself rather than calling the
base class, observing the Click event value directly and providing the
default behavior there:

protected override void OnClick(EventArgs e)
{
EventHandler handler = Click;

if (handler != null)
{
handler(this, e);
}
else
{
// implement default behavior here
}
}

That way you don't have to add an extra event to the class, and clients
can't cause trouble by still subscribing to the Click event. It does
have the downside of not calling the base OnClick method, but AFAIK all
that does is raise the event, so as long as you do that yourself, you're
fine.

Pete
Sep 14 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
9
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it...
3
by: Vajira | last post by:
Lets say there is a inheritance heirarchy like this. C3 inherit from C2 and C2 inherit from C1 ( C3 -> C2 -> C1 ). If C1 class has a public virtual member function call 'Remove()', can I limit...
3
by: mpatnam | last post by:
I have an executable which links to a static library (.a). I want to provide a hook by overriding a function part of this static library. Eg: I have a function "int blkstart(int i)" in this static...
4
by: jibran | last post by:
Hello. I have wrapped the DataGrid control with my own class (SmartDataGrid) adding some necessary functionality. My current webform has 2 SmartDataGrids. The first is populated by selected...
6
by: Mike | last post by:
Hi, I have such problem: On my form I have TabControl. I want to move from one tab to another using "Next" and "Prev" button. This part works fine. But control also supports switching between...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
6
by: Ben | last post by:
I'm having a really hard time trying to figure out how to override the drawing in my class derived from Button. When I set the text property, the drawing seems to be taking place in the set...
12
by: danil52 | last post by:
Hello there, I have the following code: class Base { public: virtual void f() {cout << "Base::f()" << endl;} virtual void f(int) {cout << "Base::f(int)" << endl;} };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.