Using web user controls is quite a common approach in developing web applications. While using them, we often get into a situation, where we need to pass some kind of data from the user control to the parent page, when any operation is performed on the user control. For ex, we may need to pass data, on OnClick event of a button on the user-control, to the parent page. In order to achieve such a functionality, delegates are one of the most helpful ways of doing this. So let’s create a sample application to discuss this approach.
Our example will consist of a Default.aspx page and ChildControl.ascx user-control. This user-control will have a button with OnClick event attached to it. On click of this button, we will send current datetime to the parent page. So for initial setup, add a button on the user-control and place the user-control on page.
To achieve this, our logic has to be, to notify the parent page that some action has been performed on the user-control. For this, our page must subscribe or attach itself to the user-control, using some kind of mechanism. This mechanism which will be the events in our case. This is similar to the concept of subscribing to newsletters or event notifications on any website. When any newsletter is published or some event happens on a website, you get an email notification of it. Similar is the case here. Whenever the event (to which parent page has subscribed) is fired, subscriber will receive notifications. So let’s start the code step wise.
Step 1 : Create a delegate on the user-control, that can encapsulate a method which takes no input and returns DateTime type. Also, declare an event on the user control, which is of the delegate type which we created.
Step 2 : Our main page will subscribe to the event that we created on the user-control, to get notification of when the event is fired on the user control.
Step 3 : Next, when the button click functionality is done, we explicitly execute the event by calling the GetDataFromChild event, passing the required data.
Step 4 : Run the application and click the button. On clicking of button, when we make the explicit call to the event from the user control, parent page receives the notification through the ucChild_GetDataFromChild method.
How it works ?
Now that we have successfully created the sample application, let’s discuss how this process is actually working. To start with, we have used the combination of event and delegates. We create a delegate of type ChildControlDelegate and an event of the same type, on the user-control. Our parent page attaches itself, to this event. Whenever the user-control finishes its task on button click, it executes the event by calling GetDataFromChild and the main page receives the notification in a method named ucChild_GetDataFromChild.
Like we discussed an example at the start of the article, when we subscribe for any newsletter on any website, we get the notification emails of it. Same is the case here.
Important point to note here is that when we attached the event on the parent page (using += syntax), it is like we are actually adding a method, which this event will execute when it is fired. This method is added with the help of a delegate. It basically acts as a receiver for the parent page. So whenever we explicitly call the event from the user-control, using the GetDataFromChild(DateTime.Now) statement, Default page will invoke the receiver method on it (which in this case is ucChild_GetDataFromChild ) to get receive the results from the user control.
So this was how we can pass data from user-control to parent page. Hope you enjoyed reading it…!!!