Modifying code of standard web parts

  Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic! Mail us feedback on this topic!  

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

 

The following example shows how you can send a 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 its base and adds a custom handler to the OnAfterSave event.

 

1. Create a copy of the BizForm web part in Kentico CMS. Go to Site Manager -> Development -> Web parts -> BizForms -> BizForm. Click CloneReport Clone web part and enter the following values:
 

Display name: BizForm with custom e-mail
Code name: BizFormWithEmail
Category: BizForms
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 and edit the CMSWebParts/BizForms/bizformwithemail.ascx file.

 

3. Switch to the Design tab and drag and drop a Label control onto the page. Set its ID to lblConfirmationMessage and clear its Text property.
 

4. Click the BizForm control and choose Events in the Properties window. Double-click the OnAfterSave event, which will create the viewBiz_OnAfterSave() method, and modify it to look like this:
 

[C#]

 

protected void viewBiz_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.viewBiz.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 viewBiz.BasicForm.DataRow property.

 

Use valid e-mail addresses and save all changes.
 

5. 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 zoneCenter zone. Set the Form name property to the Contact us form (if you're using the sample Corporate Site).
 

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 to create a modification of a standard web part.

 

Page url: http://devnet.kentico.com/docs/5_5r2/devguide/index.html?modifying_the_code_of_standard_web_parts.htm