Why is raising event from C# User Controls so hard?
Raising events from user controls is straightforward in VB; this blog asks why it should be so much harder using C#.

Posted by Andy Brown on 17 October 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Another Nail in the C# Coffin - User Control Events

One of the great pleasures of blogging is that it gives you a platform for your pet rants, and I'm going to return to one of mine: the awfulness of C#.

I've already written about this here, here and here.

To add to all of the previous points, consider how to raise events from user controls in ASP.NET.  Let's look at how you do this in VB, then compare this with C#.

The VB Method

In VB, you declare the events that you want to raise at the top of the user-control's code:

Imports System.Data

Partial Class blog_ucTaskSearch

Inherits System.Web.UI.UserControl

 

Public Event Searching(ByVal StatusId As Integer,

ByVal ContainsText As String)

When you want to raise an event, you do so by ... raising the event:

Protected Sub btnSearch_Click(ByVal sender As Object,

ByVal e As System.EventArgs) Handles btnSearch.Click

 

'search for this information

RaiseEvent Searching(StatusId, txtContains.Text)

 

End Sub

You can then write an event-handler for this event in the containing page:

Protected Sub ucTaskSearch1_Searching(ByVal StatusId As Integer,

ByVal ContainsText As String) Handles ucTaskSearch1.Searching

There's a whole lot going on here behind the scenes, but the point is that you don't need to know about this!

The C# Method

Now compare that with raising events from user controls in C#.  First you have to declare something called delegates (function pointers in old C):

using System;

using System.Collections.Generic;

using System.Data;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public delegate void ucTaskSearch_SearchHandler(

int StatusId, string ContainsText);

 

public partial class ucTaskSearch : System.Web.UI.UserControl

{

You then have to declare events which can be raised from the user control:

public event ucTaskSearch_SearchHandler Searching;

You're now finally read to raise an event from the user control (although you should first check that the event-handler has been set in the containing page):

protected void btnSearch_Click(object sender, EventArgs e)

{

// search for this information

if (this.Searching != null)

{

this.Searching(StatusId, txtContains.Text);

}

}

You then have to attach an event-handler to the user control where you declare it on the main page:

User control with event-handler

The Searching event will be handled by the SearchingHandler routine.

You can then write a routine to handle the user control's event in the main page:

public void SearchingHandler(int StatusId, string ContainsText)

{

The above approach is - to my mind - the simplest one you can take, but it's not the one recommended by C# purists (this involves creating classes which inherit from the EventArgs base class ... you don't want to know).

There - rant over!

This blog has 0 threads Add post