Modifying the code of standard web parts

This chapter explains how you can create a copy of a standard web part and modify its code.

 

The following example shows how you can send custom e-mail when a BizForm form is submitted and display a custom confirmation message. It uses the CMSWebParts\BizForms\BizForm.ascx web part as the base and adds a custom handler on the OnAfterSave event.

 

1.We will create a copy of the BizForm definition in Kentico CMS. Go to Site Manager -> Development -> Web parts -> BizForms -> BizForm. Click Clone web part and enter the following values:
 
- Web part display name: BizForm with custom e-mail
- Web part code name: BizFormWithEmail
- Web part category: BizForms
- Web part file name: BizForms/bizformwithemail.ascx
- Clone web part files: yes (checked)
 
Click Clone. The system creates a copy of the existing web part using the new name and it also copies the code (ASCX and CS file).
 
2.Now we will make the modifications to the web part. Open the web project using the WebProject.sln file in Visual Studio. Switch to the Design tab and drag and drop a Label control on the page. Set its ID to lblConfirmationMessage.
 
3.Click the BizForm control and choose Events in the Properties window. Double-click the OnAfterSave event and add the following method inside the generated method:
 

[C#]

 

protected void BizFormNew_OnAfterSave()

{

   CMS.EmailEngine.EmailMessage msg = new CMS.EmailEngine.EmailMessage();

   msg.From = "mymail@domain.com"; // use valid e-mail

   msg.Recipients = "mymail@domain.com"; // use valid e-mail

   msg.Subject = "Custom BizFrom e-mail";

   msg.Body = "The value of the FirstName field: "

                 + CMS.GlobalHelper.ValidationHelper.GetString(

                     this.BizFormNew.BasicForm.DataRow["FirstName"], "N/A");

   CMS.EmailEngine.EmailSender.SendEmail(msg);

   lblConfirmationMessage.Text = "The e-mail has been sent.";

}

 
Please notice how you can retrieve the form values through the BizFormNew.BasicForm.DataRow property.

 

Use valid e-mail addresses and save all changes.
 

4.Go to CMS Desk -> Content, choose the Home page, switch to the Design tab and add the BizForm with custom e-mail web part to the zoneBottom zone. Set the Form name property to the Contact us form (if you're using the sample Corporate Site).
 
5.Sign out and go to the live site. Enter some values into the form and submit it. You will see the additional confirmation message "The e-mail has been sent." and receive the e-mail.

 

You have seen how you can create a modification of a standard web part.