Back to Basics – Exposing UserControl Events
Back to Basics – Exposing UserControl Events
This morning I saw
Shlomo’s post
(in Hebrew) that
explain how to
expose events in a
UserControl in order
to enable the page to register to that event. This post will show
a different solution to do the same thing and also explain my opinion
in regard of whether to expose events through UserControl or not.
Should We Expose UserControl Events?
One of the things that I always explain when I’m being asked my opinion
about exposing UserControls events is that UserControls are like islands of
separation. I see them as a dumb controls which need to be striped from
business logic (including validation which in my opinion are a part of the
business logic) . What I mean in saying that is that UserControl are reusable
components that gather some visualization that is being repeated inside a
web application. After saying that probably you understand my opinion on
exposing events. I encourage to expose events from UserControls since I
prefer that the page will hold the implementation (through business logic
components) for the events and therefore making the UserControl more
reusable. When we implement the events inside the control we don’t
gain that flexibility.
Exposing UserControl Event Example
Our UserControl is going to look like:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl.ascx.cs"
Inherits="AutoCompleteExtenderTriggerButton.WebUserControl" %>
<div>
<div>
<asp:Label ID="lblUserName" runat="server" Text="User Name: "></asp:Label>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</div>
<div>
<asp:Label ID="lblPassword" runat="server" Text="Password: "></asp:Label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</div>
<div>
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
</div>
</div>
A simple login control.
The first thing to do is to create a delegate for the event we
are going to expose:
public delegate void LoginHandler(string userName, string password);
After we have the delegate we can expose the event from the
UserControl. The following example use the delegate that I showed
earlier and expose the event through the event of the login button:
public delegate void LoginHandler(string userName, string password);
public partial class WebUserControl : UserControl
{
public event LoginHandler Login;
protected void btnLogin_Click(object sender, EventArgs e)
{
if (Login != null)
{
Login.Invoke(txtUserName.Text, txtPassword.Text);
}
}
}
That is it. Now we can wire up the event in the page that the UserControl
is being hosted in.
Summary
Lets sum up, I explained a little about my opinion in regard of exposing
UserControl events. I also showed another way to expose UserControl
events. The way that Shlomo showed in his post is also applicable for
event exposing but I prefer the traditional way of creating a delegate
that inform the user of the control your intentions.
I hope that this post will help you.