Firing Event from UserControl to Parent Pageby satman
Ok the site is now back up and running completely recoded in C# from VB.net. It took about a day to convert all the pages and debug all those little issues that made me dislike C# all those years ago.

One Problem I came across was getting a simple Button control in a UserControl to fire an event back to its parent page, after trawling the net and coming across various versions all of which were a little overcomplicated I thought I would post a simple version.

In the UserControl
// eg declare an event called 'fireback' which is an EventHandler
public event EventHandler fireback;

When you want to fire the event back
if (fireback!= null) //check event is not empty safety
{
fireback(Sender, new EventArgs()); //fire event for main page
}

in the Parent Page to consume it , define the connection and the local code to execute on event detection

my_Usercontrol.fireback += new EventHandler(consumerSub);

where my_Usercontrol is whatever name you assigned to the UserControl and consumerSub is the code you fire when the event happens, for example

protected void consumerSub(object sender, EventArgs e) { ..code.. }
0 Comments #86 Friday, December 24, 2010 09:23 PM Filed in Technology
Name (Required)
Email (Required)
URI(will display as a linkback)
Comment (Required)
86 (no script tags allowed)

Custom Blog Engine Developed by Satman