Back to list of Articles
Basics of creating server controls. Part II. Events.
Author: Dmytry Rudenko aspnetman@aspnetmania.com
In my previous article I tried to explain how to build your own ASP.NET server controls. Also I promised to continue the story
about control building. Though it was a long-long time ago, one need to keep his promises, and that's what I will try to do.
In this article we will see how to add events to server control, generate postback and invoke event handlers.
So, what do we need to make our pager work? In fact, not much - we need just to define delegate, to define an event in the control and,
if necessary, to describe structure passing data to event handler. Let's start with delegate declaration.
Delegate, in fact, is a function pointer in terms of C++. Delegates, defining events in server controls, are bound with additional
restrictions - such delegate need to accept 2 parameters, first of them needs to be of the object type (it holds reference to the object
generated this event), and the second - of the EventArgs type (this parameter carries additional information, related to the invoked event).
EventArgs class - basic class for all classes that carry additional information, this class itself has no properties for passing data and is
used when the event just declares that it has happened (e.g. Click event of Button server control). In our case, it is necessary to pass
additional info to event handler - new page number. Therefore, in order to pass this info let's define a class that derives from EventArgs
class.
public class PagerEventArgs : EventArgs
{
public int SelectedPage;
public PagerEventArgs(int SelectedPage)
{
this.SelectedPage = SelectedPage;
}
}
With this, delegate declaration for our event is no longer a problem:
public delegate void PagerEventHandler(Object sender, PagerEventArgs e);
And, respectively, let's add property of this type to our class.
pre class=code>public class Pager : WebControl
{
public event PagerEventHandler Navigate;
...
}
Now we need to turn back to theory. We need our control to be able, firstly, to generate postbacks, and
secondly, to accept and handle postback info. Besides, it is necessary to send with the postback the number of the page,
click on which initiated this postback. Such possibility exists, and what is more, to implement it in our control - as easy as anything.
Do you remember, in the previous article I added spaces to href tag? Now it's time to fix it and to add client function
call generating postback. For this we can use Page.GetPostBackEventReference or Page.GetPostBackClientHyperlink methods
(these methods are practically identical, the only difference is that GetPostBackClientHyperlink adds "javascript:" prefix
in the beginning of the returned string). These methods accept 2 parameters - pointer at the control generating postback and
a parameter with additional info. In our case, the values of these parameters will be reference this and link page number.
Therefore, we will have following code outputting page number:
output.AddAttribute(HtmlTextWriterAttribute.Href, Page.GetPostBackClientHyperlink(this, (pgIndex - 1).ToString()));
output.RenderBeginTag(HtmlTextWriterTag.A);
output.Write(pgIndex.ToString());
output.RenderEndTag();
You don't need to work hard either, to get postback - you need just to implement interface IPostBackEventHandler in our control.
I.e. to use method RaisePostBackEvent of this interface. This method accepts one parameter - additional data which we defined while
calling Page.GetPostBackClientHyperlink method. Now we need just to get this data (cause namely in this parameter there is the number
of page selected) and to invoke the event.
To start with, let's define a special method checking if there is an event handler assigned and, if yes, invoking it.
This method usually has prefix On before the event name:
protected virtual void OnNavigate(PagerEventArgs e)
{
if (Navigate != null)
Navigate(this, e);
}
And only now let's implement RaisePostBackEvent method
public void RaisePostBackEvent(string arg)
{
int cmd = Int32.Parse(arg);
PagerEventArgs e = new PagerEventArgs(cmd);
CurrentPage = cmd;
OnNavigate(e);
}
That's all, with this we have finished implementation of our pager functionality.
In this article I described only a small part of issues related to the development of your own controls and said nothing about
many others aspects of this problems. But I can name them and try to show you approximate solutions. So, apart from generation of
events based on control postback, there are problems with postback data handling (IPostbackDataHandler interface), save/restore postback
controls state (LoadViewState and SaveViewState methods), problems and solutions related to implementation of large number of events in
the class (EventHandlerList class) and template controls. A big separate part in controls development is designers development. But all
these issues are subjects for separate articles which I probably will write someday
You may download complete source code here.
Back to top
|