There are now two grids displayed on one ASP page. The Departments
grid has regular gray SUBMIT buttons instead of icons. The Employee
grid has iconic buttons but they are placed on the left side of the grid.
Let's click on the "plus" button of the Employee grid:
In the edit mode the DepartmentID and MaritalStatus columns are displayed as drop-down list boxes. The DepartmentID list box was populated with the Departments table entries. The MaritalStatus list box has three fixed options, Single, Married, and Divorced which will translate to the numeric values of 1, 2, and 3, respectively (in this lesson we will not be using the Employee table's last field, FullyVested).
We can now look at the code to see how it was achieved.
| <%
Set DptGrid = Server.CreateObject("Persits.AspGrid.1") DptGrid.FileName = "Sample3.asp" DptGrid.Connect "AspGrid", "", "" DptGrid.SQL = "select id, name, phone from Departments" DptGrid.Cols(1).Hidden = True DptGrid.UseImageButtons = False Set EmpGrid = Server.CreateObject("Persits.AspGrid.1")
<HTML>
<%
</BODY>
|
As before, we will look at the ASP code line by line skipping the methods and properties we are already familiar with.
DptGrid.UseImageButtons = False is what caused the Departments grid to show gray SUBMIT buttons rather than icons.
EmpGrid.NumberOnPage = 2 is necessary if there are more that one AspGrid instance on one page. This propery (1 by default) must be set to a unique integer value for every grid on the page. This will prevent multiple grid objects to generate conflicting input variable names.
EmpGrid.ShowLeftSideButtons False will cause a grid to show the control buttons (add, delete, edit, etc) on the left side. If the optional parameter is set to True or omitted, the grid will show control buttons on both sides, otherwise, the buttons will be shown on the left side only. By default, a grid displays control buttons on the right side.
EmpGrid.Cols(2).AttachForeignTable "select id, name from Departments", 1, 2 will turn column 2 (Employee.DepartmentID) into a drop down list box with values coming from the specified SQL statement. The second parameter specifies which field in that SQL statement will serve as the database value (i.e. a value that will eventually be saved in the database field corresponding to the column to which AttachForeignTable is applied). The third parameter specifies which field will serve as a display value. In this example, Departments.id is the database value and Departments.name is the display value. After the user picks a department name, this value will be translated into the corresponding database value and saved in the Employee.DepartmentID field.
It is allowed to have the second and third parameters point to the same field. In that case the display value itself will be saved in the field corresponding to the column to which AttachForeignTable is applied.
The values for a drop-down list box do not have to come from a database table. They can also be defined as a fixed array:
EmpGrid.Cols(6).Array
= Array("Single", "Married", "Divorced")
EmpGrid.Cols(6).VArray
= Array(1, 2, 3)
These two lines have an almost identical effect to AttachForeignTable, except that the values for the drop-down list box come from fixed arrays. The display values are specified via the Array property, and database values via the VArray property. If the VArray propery is not set, the display value itself will be saved in the database.
EmpGrid.Cols(5).FormatNumeric 2, True will format the numeric values displayed in column 5 (Employee.Salary). The first parameter specifies the amount of decimal places in the formatted number. The second parameter specifies whether commas separators should be used.