Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 374
ArtK Avatar asked Feb 25 '14 22:02

ArtK


2 Answers

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";
}
like image 128
Yousuf Avatar answered Sep 23 '22 14:09

Yousuf


Because of the ASP.NET webpage lifecycle I get Nulls when I set public properties on the user control from the main page. Those variables go out of scope once the page is sent to the client. However, here is an easy solution that will let you get notified when an event happens in your user control.

NOTE: There are some issues with Page.Context when back in the main page so changing things that effect the viewstate might not work.

Inside my user control i did the following...

 // UC - setting up delegate and usercontrol property to store it
 public delegate void CallbackAction(object sender, CustomEventArgs e);
 public CallbackAction OnCallbackAction
 {
    get { return Session["CallbackAction"] as CallbackAction;  }
    set { Session["CallbackAction"] = value; }

 }

Somewhere in the UserControl an event happens that you want to notify the main page of

// UC - callback from within the usercontrol
protected void UserControlButton_Click(object sender, EventArgs e)
{
   if (IsPostback)
   {
      // do some processing
      if (this.OnCallbackAction != null)
      {
         this.OnCallbackAction.Invoke(this, new CustomEventArgs("ET phone home") );
      }
   }

}

The structure above will now callback a main page that registers with the usercontrol. Here is how to register with the usercontrol from the main page

// Main Page - load event 
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostback)
   {
      // register with usercontrol
      this.UserControlFoo.OnCallbackAction += CallbackFromUserControl_Click;
   }
}

// Main Page - this event will get called back when ET phones home
protected void CallbackFromUserControl_Click(object sender, CustomEventArgs e)
{
   // phoned home from usercontrol
}
like image 41
Juls Avatar answered Sep 21 '22 14:09

Juls