Modifying the code or design of standard web parts

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

 

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, click BizForms, click New web part and enter the following copy of the original BizForm properties:
- Web part display name: BizForm with custom e-mail
- Web part code name: BizFormCustomEmail
- Web part file name: BizForms/BizFormCustomEmail.ascx
 
Click OK. Now we need to re-create the BizForm web part properties. Switch to the Properties tab and create a new category BizForm settings. Then create the following fields:
 
- Attribute name: BizFormName
- Attribute type: Text
- Attribute size: 100
- Field caption: Form name
- Field type: BizForm selector
 
- Attribute name: UseColonBehindLabel
- Attribute type: Boolean (Yes/No)
- Field caption: Use colon (:) in labels
- Field type: Checkbox
 
2.Now, we will create a copy of the BizForm.ascx user control. Open the web site project in Visual Studio 2005 and unfold the folder CMSWebParts/BizForms. Right-click the bizform.ascx user control and choose Copy. Right-click the BizForms folder and choose Paste. Rename the newly created user control to BizFormCustomEmail.ascx.
 
It's also necessary to update the following settings in the code:
- open the Source view of the control and set the Inherits attribute value on the first line to Inherits="CMSWebParts_BizForms_BizFormCustomEmail"
- switch to the code behind and change the class name to CMSWebParts_BizForms_BizFormCustomEmail
 
3.Now we will make the modifications to the web part. Switch to the Design mode and drag and drop a Label control on the page. Set its ID to lblConfirmationMessage.
 
4.Switch to the code behind and add the following method to the class:
 

      [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 note how you can retrieve the form values through the BizFormNew.BasicForm.DataRow property. Save all changes.
 

5.Go to CMS Desk -> Content, choose the Home page, switch to the Design mode and add the BizForm with custom e-mail web part to the zoneBottom zone. Set the Form name property to the Contact us form.
 
6.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.