|
||
This topic explains how you can create a copy of a standard web part and customize its behavior by modifying its code.
The following example uses the Forms -> On-line form web part as its base. It shows how you can send a custom e-mail when a form is submitted and display a confirmation message. Please note that this is only for demonstration purposes. There is a much easier way to set up notifications or autoresponders for on‑line forms through the built‑in functionality of the Forms module.
1. Create a copy of the On-line form web part in Kentico CMS. Go to Site Manager -> Development -> Web parts and select the Forms -> On-line form web part from the tree. Click the Clone web part action and enter the following values:
•New object display name: Form with custom e-mail
•New object code name: FormWithEmail
•Clone web part to category: Forms
•Clone web part files: yes (checked)
•Cloned web part file name: BizForms/formwithemail.ascx
Click Clone. The system creates a copy of the existing web part and its code files (ASCX and CS) using the specified names.
2. Open the web project in Visual Studio using the WebProject.sln (or WebApp.sln) file and edit ~/CMSWebParts/BizForms/formwithemail.ascx.
|
Important!
If you installed Kentico CMS as a web application, the clone's code files will not be visible right away, since they are not included in the project. In this case:
1. Click Show all files at the top of the Solution Explorer. 2. Right-click the formwithemail.ascx file in the BizForms folder. 3. Select Include in Project. |
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. Edit the code behind file and locate the viewBiz_OnAfterSave handler, which is executed every time the form is saved. Insert the following code after the default content of the method:
[C#]
void viewBiz_OnAfterSave(object sender, EventArgs e) CMS.EmailEngine.EmailMessage msg = new CMS.EmailEngine.EmailMessage(); |
This code creates a new e‑mail message, sends it out and adds text into the confirmation label. You can notice how the content of the e‑mail dynamically retrieves a field value from the current form by using the BasicForm.GetDataValue(string fieldName) method, which may be called through the corresponding property of the BizForm control. Fill in valid e‑mail addresses into the code to be able to try out the example.
Save all changes. Remember to Build the project if it is installed as a web application.
5. Go to CMS Desk -> Content, choose the Home page, switch to the Design tab and add the Form with custom e‑mail web part to the Main zone zone. Set the Form name property to the Contact Us form using the Select button. This form should be available by default if you are working with the sample Corporate Site, and it includes the FirstName field used in the custom code.
6. Finally, open the live site and view the Home page. Enter some values into the form and submit it. You will see the additional confirmation message ("The e-mail has been sent.") and the specified address will receive the e-mail.
You have seen how to create a customized version of a standard web part. Using the same approach, you can modify the ASPX markup or code behind of any of the default web parts in order to alter their functionality to fit your specific requirements.