Add event delegate to event handler of ASP.NET user control

ArtK picture ArtK · Feb 25, 2014 · Viewed 13k times · Source

I have a web user control in asp.net - a keypad. It has buttons for all digits, a textbox for display of input and clear and submit buttons. The user control sits in AJAX update panel so it can be easily shown/hidden. (App is destined for tablet browsers) I need to add a delegate to subMitButton ClickEventHandler inside my user control inside AJAX update panel. Any suggestions? Is it even possible? I don't want to use tablet's soft numeric keyboard because it is way to big. I could break my user control apart and use its code inside my web site but I'd rather use user control as intended.

Answer

Yousuf picture Yousuf · Feb 26, 2014

you can raise an event from usercontrol which can be handled in the page .

You can declare a event

public event EventHandler SubmitButtonClick;
 protected void OnSubmitButtonClick()
{
    if (SubmitButtonClick!= null)
    {
        SubmitButtonClick(this, new EventArgs());
    }
}

And then on the page, handle the event .

  WebUserControl1.SubmitButtonClick+= 
    new WebUserControl.EventHandler(WebUserControl1_SubmitButtonClick);
 private void WebUserControl1_SubmitButtonClick(object sender, EventArgs e)
{
    Label1.Text = "Button Pressed";
}