Custom Table Event Handler

Aaron Hayon asked on December 22, 2015 23:20

I am currently leveraging a custom table form to save some information. After submitting the data, I want the from to redirect to another page. I have this working in one scenario where I use the following code

 private void CustomTable_InsertAfterHandler(object sender, CustomTableItemEventArgs e)
    {
        CustomTableItem item = e.Item;

        if (item != null && item.CustomTableClassName.ToLower() == "customtable.expocontact")
        {
            int expoContactID = item.GetIntegerValue("ExpoContactID", 0);
            SessionHelper.SetValue("ExpoContactID", expoContactID);
            URLHelper.ResponseRedirect("~/Expo-Registration/Expo-Attendee.aspx");
        }
        else if (item != null && item.CustomTableClassName.ToLower() == "customtable.expoattendee")
        {
            int expoAttendeeID = item.GetIntegerValue("ExpoAttendeeID", 0);
            URLHelper.ResponseRedirect("~/Expo-Registration/Expo-Attendee-List.aspx?ExpoAttendeeID=" + expoAttendeeID);
        }
    }

I am running into an issue however when going through the else if statement for ExpoAttendee. In looking at the debug information in Kentico, it is calling the insert method but then does a rollback transaction. I am assuming this is because the redirect is happening prior to the database being completely updated.

Is there a way to ensure that the Insert has completed prior to calling the redirect? I stumbled upon the CallWhenFinished method off of the CustomTableItemEvent which I am going to try. Is there any other way to do this?

Recent Answers


Aaron Hayon answered on December 22, 2015 23:44

I attempted the CallWhenFinished method but it created the same result. The redirect happened prior to the insert being finished so the transaction was rolled back. I would have thought the InsertAfter method would only be fired after the transaction had been completed. This almost seems like a bug within the CMS.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on December 23, 2015 06:07

The URLHelper.ResponseRedirect(url) inhertis the standard Response.Redirect(url) method, which aborts the current thread so I can see why it is causing you problems. I don't think it's a bug, I think you need to perform those actions within your webpart in the customTableForm_OnAfterSave event.

The problem with performing the particular actions you are doing (redirect) in a global event handler is these will be fired anytime the insert is happening whether you're in the Kentico UI or not. It seems like your requirements are pretty specific which is why I'd clone the webpart and do what you need within there.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.