Try our conversational search powered by Generative AI!

XForm Block problems

Vote:
 

I want to alter the form and add some functions to send mail in BeforeSubmit and in AfterSubmit I want to do a redirect to a certainpage.

How do I solve this? 

In XFormBlock.ascx.cs I have the following code;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!IsPostBack && CurrentPage is CalendarEventPageType && CurrentBlock.Form != null)
            {
                
                XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);

            }
            
        }

        protected void XForm_ControlSetup(object sender, EventArgs e)
        {

            var control = (XFormControl)sender;
            control.BeforeSubmitPostedData += new SaveFormDataEventHandler(XForm_BeforeSubmitPostedData);
            control.AfterSubmitPostedData += new SaveFormDataEventHandler(XForm_AfterSubmitPostedData);

        }

   The events run fine and works. However! Everytime the page containing the xform block is rendered it will register a

new EventHandler(XForm_ControlSetup);

which means that after entering any calendarEventPage for say 10 times and I finally post a form the BeforeSubmit event will run 10 times.

Is it possible to somehow check if the events allready been registered? 

#81910
Feb 28, 2014 11:54
Vote:
 

Do unsubscribe before subscribe :)

XFormControl.ControlSetup -= new EventHandler(XForm_ControlSetup);
XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);

    

#81915
Feb 28, 2014 12:36
Vote:
 

Thank you, but unfortunately I have already tried that. :-(

#81920
Feb 28, 2014 13:08
Vote:
 

Ok, Can't you subscribe to Before | After posted data events directly to CurrentBlock.Form property and not throught static ControlSetup event?

#81931
Feb 28, 2014 14:24
Vote:
 

You should listen to static events only on application start, for example in Global.asax.cs as shown below:

 protected void Application_Start(Object sender, EventArgs e)
{
            XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);
}

    

#81951
Edited, Feb 28, 2014 16:00
Vote:
 

Though a better practice use to be that you add listeners in an initializationmodule instead of global asax

#81952
Feb 28, 2014 16:24
Vote:
 
#83106
Mar 25, 2014 23:21
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.