How to Pass UserControl events up to the Web Page


As we know a UserControl has its own UI and functionality which can be reused across multiple web pages by just dragging it and dropping it in the DesignView of the intended Web form. Often the requirement is to Pass the event of the control (Ex:Button control) to the webpage in which the usercontrol is being used. In this article we are going to see how we can pass the event of a control embedded in a UserControl to be passed to the webpage in which it is used.

In this article we are going to see how we can pass the event of a control embedded in a UserControl to be passed to the webpage in which it is used.

As we know a UserControl has its own UI and functionality which can be reused across multiple web pages by just dragging it and dropping it in the DesignView of the intended Web form. Often the requirement is to Pass the event of the control (Ex:Button control) to the webpage in which the usercontrol is being used.

Let us follow below steps to Pass UserControl's Button event up to the Web Page:
Step 1
Write a event arguments class, which inherits from EventArgs. This class will be used by the event to pass data from the user control to the page. Then add properties to the class which provide data from the usercontrol to the page.


public class DataEventArgs : EventArgs
{
public DataEventArgs(string message)
{
this.Message= message;
}
public string Message{ get; private set; }
}

Step 2
In the UserControl class, declare an event as shown below:

public event EventHandler SubmitButtonClick;

Step 3
Write the code to handle Button click event. Inside this Button click event, we shall raise the event back to the consuming page. With this, whichever page uses our usercontrol can get notified about the event and respond accordingly. The following code shows the button Click event handler.

protected void btnSubmit_Click(object sender, EventArgs e)
{
if (SubmitButtonClick != null)
{
SubmitButtonClick(this, new DataEventArgs(TextBoxAddress1.Text));
}
}

Step 4
The final step is, Whichever Page has used the UserControl, on that page we have to consume the event.

Once you add the user control to the page, you must write code to consume the event raised by the user control and for this you need to attach the event handler by using the += syntax in the Page_Init method of the Web Page. In our event handler, we will use DataEventArgs as required. For simplicity I am displaying the value in the Message property of the DataEventArgs class.

protected void Page_Init(object sender, EventArgs e)
{
UserControl1.SubmitButtonClick += this.UserControl1_SubmitButtonClick;
}
private void UserControl1_SubmitButtonClick(object sender, DataEventArgs e)
{
//Do processing with the data you get here.
Response.Write(e.Message);
}


Article by Vaishali Jain
Miss. Jain Microsoft Certified Technology Specialist in .Net(Windows and Web Based application development)

Follow Vaishali Jain or read 127 articles authored by Vaishali Jain

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: