In order to capture events on buttons in ASP.NET web forms that are part of a WebUser Control, the following method without delegate can be used:
a. Code behind of Web User Control
//Create an event handler
public event EventHandler OnButtonClick;
//Create an new event on the click of button
public void Button1_Click(object sender, EventArgs e)
{
this.OnButtonClick(this, new EventArgs());
}
b. Code behind of Web Form of ASP.NET
//On the page load event use the following code
this.WebUserControl1.OnButtonClick+=new EventHandler(WebUserControl1.OnButtonClick);
//Define the method that handles the event now
void WebUserControl1.ButtonClick(object sender, EventArgs e)
{
///custom code goes here.
}
This is the easiest method of generating an event and capturing the same in the main Web Form without Delegates. We can do the same with the help of delegates. I will cover the same in the coming post.
a. Code behind of Web User Control
//Create an event handler
public event EventHandler OnButtonClick;
//Create an new event on the click of button
public void Button1_Click(object sender, EventArgs e)
{
this.OnButtonClick(this, new EventArgs());
}
b. Code behind of Web Form of ASP.NET
//On the page load event use the following code
this.WebUserControl1.OnButtonClick+=new EventHandler(WebUserControl1.OnButtonClick);
//Define the method that handles the event now
void WebUserControl1.ButtonClick(object sender, EventArgs e)
{
///custom code goes here.
}
This is the easiest method of generating an event and capturing the same in the main Web Form without Delegates. We can do the same with the help of delegates. I will cover the same in the coming post.
Comments