ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

New WEBDEV 2024 feature!
Help / WEBDEV Tutorial / Tutorial - Creating a website with back office processes
  • Lesson 3 - Editing products
  • Overview
  • A new page to edit products
  • Creating the page
  • What is this page for?
  • Creating Edit controls
  • Displaying data in the page
  • Confirming changes in the page
  • Opening the form from the list of products
  • Testing changes made to a product
  • To sum up

Tutorial - Creating a website with back office processes

Lesson 3 - Editing products
We will cover the following topics:
  • Creating a product form page in Session mode.
  • Editing a product.
Durée de la leçon 50 mn
Overview
In the previous lesson, we created the list of products. Now, let's create a new page for viewing and editing product details.
In the next lesson we will see how to add a product.
Opening the example project:
In WEBDEV, open the "Full_WEBDEV_Site" project:
  1. Go to the WEBDEV home page (Ctrl + <).
  2. Click "Tutorial", then in "Tutorial - Create a WEBDEV website (Back Office and Front Office)", double-click "Full WEBDEV Site - Exercise".
Warning
This project will be used in the different lessons of this tutorial.
A new page to edit products
Now that the list of products is displayed, we may want to edit a given product. We will create a page to view and edit the product details.

Creating the page

We will follow the same steps as before to create the page:
  1. Click Create an element in the quick access buttons.
  2. In the "New" window, click "Page" then "Page.
  3. In the page creation wizard, under "Based on a project template", select "PAGETPL_Menu".
  4. Finish the wizard.
  5. In the save window, enter the page title: "Product form". The page name "PAGE_Product_form" is automatically filled in.
  6. Validate.

What is this page for?

In this page, users should be able to edit the characteristics of the product currently selected in the Looper control. This type of page is called "Form" because it corresponds to a descriptive form of the desired element.
In our case, this page will display the contents of the different items from the "Product" data file.
First of all, we need to indicate which product will be displayed. To do so, simply declare a parameter in the page.
To declare a parameter in the page:
  1. Press F2. The code editor displays the different page events.
  2. In the "Global declarations" event, replace the existing WLanguage code with the following:
    Global declarations of PAGE_Product_form (server)
    PROCÉDURE MyPage(NumProductToDisplay is 8-byte int)
  3. Let's take a look at this WLanguage code:
    • The PROCEDURE keyword in the "Global declarations" event is used to associate a procedure to the opening of the page.
    • The name of the procedure is "MyPage". At runtime, this keyword will be automatically replaced with the name of the current page.
    • The procedure expects the NumProductToDisplay variable (8-byte integer) as parameter. This variable corresponds to the product identifier that will be displayed in the page. This variable is of the same type as the corresponding ProductID item described in the Product data file.
  4. Close the code editor.

Creating Edit controls

We will create the Edit controls that will display the details of the selected product.

To create the controls in the page:
  1. Open the "Analysis" pane if necessary: in the ribbon, on the "Home" tab, in the "Environment" group, expand "Panes", click "Panes", and then select "Analysis". The different data files defined in the "Full_WEBDEV_Site" analysis are displayed in the pane.
  2. Click the arrow on the left of the "Product" data file: the items in the data file appear.
    Analysis pane
  3. Use the mouse to select all the items displayed in the pane (except "ProductID"). You can select multiple elements by holding the Ctrl key down.
  4. Drag the items and drop them onto the previously created page.
    Drag and drop items
  5. Different controls are automatically created in the page. These controls are bound to the corresponding item in the data file. To check this:
    • Select the "Reference" control for example.
    • Right-click to open the context menu and select "Description".
    • Go to the "Binding" tab in the description window:
      Reference control description ('Binding' tab)
      This tab shows that the current Edit control is bound to the "Reference" item in the "Product" data file.
  6. Close the description window ("OK" or "Cancel").
  7. Save the page (Ctrl + S).
Move the controls in the page to get the following UI:
Interface of the Product form
  • Place the image to the right of the Edit controls.
  • Move the "Arrival" control below the Edit controls

Displaying data in the page

The "Form" page will display the product selected in the Looper control. In the WLanguage events of the page, we will write the code to:
  • find the product to display.
  • display data in the page.
To search for data and display it in the page:
  1. Click the page in the editor.
  2. Press F2. The code editor displays the different page events.
  3. In the "Global declarations" event, write the following code below the code previously written:
    Global declarations of PAGE_Product_form (server)
    Product.ReadSeekFirst(ProductID, NumProductToDisplay)
    IF Product.Found() = False THEN
    // Display the list of products
    PAGE_List_of_products.Display()
    END
    Product.ToPage()
  4. Let's take a look at this WLanguage code:
    • <Data file>.ReadSeekFirst searches for the first record in the Product data file for which the ProductID item is equal to the value of NumProductToDisplay. NumProductToDisplay corresponds to the parameter passed to the page.
    • <Data file>.Found checks if a record has been found. This function is mainly used in sites accessed by multiple users. It allows you to avoid errors caused by other users (deleted records, etc.). In this case, if the record is not found (<Data file>.Found returns False), the list of products is displayed again.
    • <Source>.ToPage gets the data from the current record in the data file and displays it in the page controls. In our case, the current record is the record found by <Data file>.ReadSeekFirst.
  5. Close the code editor.

Confirming changes in the page

The "PAGE_Product_form" page will allow you to edit a product from the list of products.
We will now add a Button control to confirm the changes made in the page: the "OK" Button control will check the entered data and save changes.
To create the "OK" Button control:
  1. On the "Creation" tab, in the "Usual controls" group, click "Button".
  2. Then, click in the center of the page, under the "Arrival" control, to create the new control.
  3. Change the control text: type "OK".
    Product form with the Button control
    The "OK" button will be used to:
    • check the data typed: this consists in checking whether the user has filled in the various fields of the page. This check doesn't require any server access and can be done in Browser code.
    • save the data entered in the Product data file. This operation is performed in Server code. The data is sent to the server, and then saved in the data file.
Write the code of the "OK" button:
  1. Select the "OK" button and press F2. The events associated with the Button control are displayed.
  2. In the "Click xxx (browser)" event of the Button control, write the following code for checking the data entered:
    Click on BTN_OK (onclick browser event)
    IF EDT_Reference ~= "" THEN
    Error("Enter a reference.") 
    SetFocusAndReturnToUserInput(EDT_Reference)
    END
    IF EDT_Caption ~= "" THEN
    Error("Type a caption.")
    SetFocusAndReturnToUserInput(EDT_Caption)
    END
    IF EDT_Description ~= "" THEN
    Error("Enter a description.")
    SetFocusAndReturnToUserInput(EDT_Description)
    END
    IF EDT_PriceBT = 0 THEN
    Error("Type a price.")
    SetFocusAndReturnToUserInput(EDT_PriceBT)
    END
  3. Let's take a look at this WLanguage code:
    • For each Edit control in the page, we check if a value has been entered.
    • The '~=' operator checks whether the values are equal ignoring the case and punctuation.
    • If no value is entered, an error message prompts the user to enter a value (Error). The code execution is stopped and the input focus is set on the relevant Edit control with SetFocusAndReturnToUserInput.
  4. In the "Click xxx (server)" event of the Button control, write the following WLanguage code for saving the data:
    Click on BTN_OK (server)
    Product.FromPage()
    Product.Modify()
    PAGE_List_of_products.Display()
  5. Let's take a look at this WLanguage code:
    • <Data file>.FromPage updates the items of the current record with the data from the bound controls. This function is equivalent to the following lines of code:
      Product.Reference = EDT_Reference
      Product.Caption = EDT_Caption
      Product.Description = EDT_Description
      ...

      Our page uses less than 10 controls and the advantages are clear. Think of the pages that contain many more controls: a single line of code assigns all the values!
    • <Data file>.Modify updates the current record in the data file.
    • <Page>.Display displays another page. In our case, "PAGE_List_of_products" is redisplayed.
  6. Save changes (Save an element or Ctrl + S).
  7. Close the code window.
An icon appears at the upper-right corner of the "OK" button. This icon indicates that there is a UI error or a potential improvement. Hover over the icon for more details. In our case, WEBDEV suggests associating the button with the Enter key. Let's accept this suggestion:
  • Click the icon.
  • In the window that appears, click "Fix".

Opening the form from the list of products

Now, let's see how to open the form of the product selected in the list. It is very simple: the user will select the product in the Looper control and view the details via a Link control.

We will modify "PAGE_List_of_products" to create a Link control:
  1. Select the "List of products" page: click "PAGE_List_of_products" in the open document tabs:
    Open document tabs
  2. Reduce the Reference control horizontally: We will create the Link control next to it.
  3. On the "Creation" tab, in the "Usual controls" group, click "Link".
  4. Then, click next to the reference in the Looper control to create the Link control.
    Positioning the Link control
  5. Change the text of the Link control (press Space to edit the text): enter "Edit".
The "Edit" Link control must open "PAGE_Product_form". We will open this page programmatically.
  1. Select the "Edit" Link control and open the associated WLanguage events (F2).
  2. In the code window that appears, write the following code in the "Click xxx (server)" event:
    Click on LINK_Edit (server)
    PAGE_Product_form.Display(ATT_ProductID[LOOP_Product])
  3. Let's take a look at this WLanguage code:
    • <Page>.Display opens "PAGE_Product_form".
    • The opened page expects as a parameter the identifier of the product to be displayed. This identifier corresponds to the identifier of the product selected in the Looper control. To get the identifier, the attribute that contains the identifier (ATT_ProductID) for the current row is specified. The square brackets are used to specify the row and LOOP_Product gets the current row in the Looper control.
      By default, ATT_ProductID returns the value of the attribute for the clicked row. The code can be written as follows:
      PAGE_Product_form.Display(ATT_ProductID)
  4. Save changes (Save element or Ctrl + S).
  5. Close the code window.
Testing changes made to a product
We have created the different elements to edit the products. Now we will test them.
Test the project (click Test project in the quick access buttons).
  1. The editor prompts you to select the first page of the Session mode.
    In our case, select "PAGE_List_of_products" and confirm.
    The first page of Session mode is the first page displayed when the site is opened in Session mode.
    The first project page in Session mode can be defined:
    • when testing the project.
    • in the "Project explorer" pane: simply select the desired page, open the context menu and select "First page of the project in session mode".
    When you define the first page of the Session mode, a small red "1" appears to the left of the page name in the "Project explorer" pane.
  2. The website opens.
  3. In the list of products:
    • Click the "Edit" link.
    • The page with the product details opens.
      Product details page
    • Change the price of the product and click OK to confirm changes.
    • The new price of the product appears in the list of products.
Close the pages to stop the test.
To sum up
In this lesson, we discovered how to manipulate web-specific controls (Looper control) to display data coming from a database. We have also created a page to view data from a record.
In the next lesson, we will see how to edit a record and improve the website design.
Completed project
If you want to check the end result of the steps described here, you can open a completed version of the project. This project contains the different pages created in the lessons of this part. This project illustrates the expected end result. To open the completed project, go to the WEBDEV home page and click "Tutorial", then in "Tutorial - Create a WEBDEV website (Back Office and Front Office)", double-click "Full WEBDEV Site - Answers".
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 2024
Comments
Click [Add] to post a comment

Last update: 12/11/2023

Send a report | Local help