Validators in a DataGrid |
By Doug Seven Published: 6/4/2002 Reader Level: Beginner Rated: 5.00 by 3 member(s). |
|
<script language="JavaScript" type="text/javascript"> var axel = Math.random(); var num = axel * 100000000000000000 + "?"; NS4 = document.layers ;if (NS4) {origWidth = innerWidth;origHeight = innerHeight;} function reDo() {if (innerWidth != origWidth || innerHeight != origHeight) location.reload();} if (NS4) onresize = reDo; </script> <script language="Javascript1.1" type="text/javascript"> document.write(''); </script>
The Question: I'm reading and testing your examples of validators, but I'd like to insert a RangeValidator control in a DataGrid control using range values from database... How can I do that??? The Answer: This may be easier than you think. Since the validator control expose properties like any other Web server control, you can set those properties either declaritively or programmatically. In other words, you can set the properties inline in the control tag, or in code. For this example I'll show you how to use the DataGrid EditItemTemplate (from the TemplateColumn) and place a number of validators with their validation properties set either declaratively or programmatically. The code for this tutorial is available in both C# and Visual Basic .NET. Adding Ranges to Northwind To begin with I need to add a table to the Northwind database - the Ranges table. I am going to be building an editable DataGrid that shows the Products from the Northwind database, and the Ranges table will be used to specify the minimum and maximum ranges for the ReorderLevel field in the Products table. Figure 1 shows the Ranges table structure and the data in it. Figure 1 - The Ranges Table The Ranges table provides minimum and maximum Reorder Levels for each category of products in the Northwind database. Building the EditItemTemplate I am not going to go into how to build a DataGrid with an EditItemTemplate - for a tutorial on that topic, see Working with DataGrid Templates. Rather, I will just jump right into how I have cinstructed the ItemTemplate and the EditItemTemplate. Listing 1 shows the Template column for my DataGrid. In the ItemTemplate I render the Product information, such as the Quantitty Per Unit, Unit Price, Units In Stock, Units On Order, and Reorder Level. In the EditItemTemplate I use TextBox controls to render the values of each database field. This enables the user to change the data. Listing 1
For the UnitPrice and ReorderLevel fields I have added three validator controls, a RangeValidator, CompareValidator and RequiredFieldValidator. Combined these validators are used to ensure that a value is entered in the TextBox and that the value is numeric and within a specified range. For the UnitPrice TextBox (txtUnitPrice) the RangeValidator is set to validate within the range 1-100. The ReorderLevel RangeValidator's properties are set based on values in the Ranges table. Listing 2 shows the BindData() method in the Web Form codebehind class, which includes the SQL SELECT command that brings the data to the DataGrid.
Setting the RangeValidator Properties From Database Data For the ReorderLevel field I used standard databinding expressions to set the MinimumValue and MaximumValue properties of the RangeValidator, as well as dynamically construct the error message that is displayed to the user. Using standard databinding expressions I am able to set the properties of the Range validator. The results are shown in Figure 2.
Figure 2 - The DataGrid in Use Summary Any Web Server control can have its properties set either declaratively or programmatically. The validator controls are no exception. In this tutorial I showed you how to include validator controls in an EditItemTemplate of a DataGrid and set the validator properties either declaratively or programmatically. |