I have a web part that I use to add some custom controls to the New, Edit, and Display forms for a SharePoint ListItem. I added a handler for SPContext.Current.FormContext.OnSaveHandler to update the item. I add my web part to Edit and New Forms using SharePoint Designer and hide (set IsVisible = False) for the DataFormWebPart, which is placed by default. Everything works great when editing an item. My OnSaveHandler function is called and I am updating SPListItem. The problem is a new item. The OnSaveHandler function is not called if I do not see DataWebFormPart. I do not make any other changes to the web form, but I switch the visibility of the DataFormWebPart. Any ideas what I'm doing wrong?
if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit ||
SPContext.Current.FormContext.FormMode == SPControlMode.New)
{
SPContext.Current.FormContext.OnSaveHandler += FormContext_OnSave;
}
....
protected void FormContext_OnSave(object sender, EventArgs e)
{
}