.Net All About

.Net All About

User Control Events


Communication between a user control and a web page can occur through
events.
With methods and properties, the user control reacts to a change made by the web
page code. With events, the story is reversed: the user control notifies the web page about an
action, and the web page code responds.
Creating a web control that uses events is fairly easy. In the following example, you’ll see a
version of the LinkMenu control that uses events. Instead of navigating directly to the appropriate
page when the user clicks a button, the control raises an event, which the web page can
choose to handle.

The first step to create this control is to define the events. Remember, to define an event,
you must first choose an event signature. The .NET standard for events specifies that every
event should use two parameters. The first one provides a reference to the control that sent
the event, while the second incorporates any additional information. This additional information
is wrapped into a custom EventArgs object, which inherits from the System.EventArgs
class. (If your event doesn’t require any additional information, you can just use the predefined
EventArgs class, which doesn’t contain any additional data. Many events in ASP.NET,
such as Page.Load or Button.Click, follow this pattern.)

The LinkMenu2 control uses a single event, which indicates when a link is clicked:
public partial class LinkMenu2 : System.Web.UI.UserControl
{
public event EventHandler LinkClicked;

}

This code defines an event named LinkClicked. The LinkClicked event has the signature
specified by the System.EventHandler delegate, which includes two parameters—the event
sender and an ordinary EventArgs object. That means that any event handler you create to
handle the LinkClicked event must look like this:

protected void LinkMenu_LinkClicked(object sender, EventArgs e)
{ … }

This takes care of defining the event, but what about raising it? This part is easy. To fire the
event, the LinkMenu2 control simply calls the event by name and passes in the two parameters,
like this:

// Raise the LinkClicked event, passing a reference to
// the current object (the sender) and an empty EventArgs object.

LinkClicked(this, EventArgs.Empty);

The LinkMenu2 control actually needs a few more changes. The original version used the
HyperLink control. This won’t do, because the HyperLink control doesn’t fire an event when
the link is clicked. Instead, you’ll need to use the LinkButton. The LinkButton fires the Click
event, which the LinkMenu2 control can intercept, and then raises the LinkClicked event to
the web page.

The following is the full user control code:

public partial class LinkMenu2 : System.Web.UI.UserControl
{
public event EventHandler LinkClicked;
protected void lnk_Click(object sender, EventArgs e)
{
// One of the LinkButton controls has been clicked.
// Raise an event to the page.
if (LinkClicked != null)
{
LinkClicked(this, EventArgs.Empty);
}
}

Happy Programming…

August 1, 2009 - Posted by | Asp.net |

1 Comment »

  1. magnificent points altogether, you simply received a brand new reader.
    What could you recommend about your submit that
    you simply made a few days ago? Any certain?

    Comment by rhinestone | December 12, 2012 | Reply


Leave a comment