I'm currently going through this book a second time and re-building several of the TYOs using VS.NET, and splitting out the
vb script into code behind files, just so I can get into the habit of doing it that way regularly. (As an aside, the VS.NET environment is so rich that I can't imagine anyone building ASP.NET apps any other way.)
But I have to say that the validation TYO on pp254-255 is really lame. It doesn't trap non-numeric errors! It simply states (in Step 3, at the top of page 3), that "any attempt to update a price with a value that's **not sensible** will result in a friendly error message".
Afraid not! Entering a letter or other nonnumeic character in a field intended for numeric input is a very common user mistake! In this example, a nonnumeric input generates a very ugly server error page.
I tried tweaking the
vb script as follows but it didn't work:
Dim pricetextbox As TextBox = CType(e.Item.Cells(2).Controls(0), TextBox)
Dim pricevalue As VariantType = pricetextbox.Text
'validation code
'if the value is not numeric, raise the error message
If (Not (IsNumeric(pricevalue))) Then
loadgrid()
With lblError
.Text = "Please enter a positive numeric value for Product Price."
.ForeColor = Drawing.Color.Red
.Visible = True
End With
' if the value IS numeric,
ElseIf IsNumeric(pricevalue) Then
'convert it to a decimal;
Dim myprice As Decimal = Convert.ToDecimal(pricevalue)
'if it's a negative value or zero, raise the error
If myprice <= 0 Then
loadgrid()
With lblError
.Text = "Please enter a positive numeric value for Product Price."
.ForeColor = Drawing.Color.Red
.Visible = True
End With
'if the value IS a positive decimal, perform the update
ElseIf myprice > 0 Then
'this will re-render the grid without any input controls
dgProducts.EditItemIndex = -1
lblError.Visible = False
'this function is defined below
UpdateProduct(ProductID, myprice)
End If
End If
Validation and error handling are so important that there should be a totally separate Chapter "7A" devoted to these topics. Not only that, but the don't even tie this example back to the discussion of asp validation controls in Chapter 6. In general, these topics are given really short shrift in this book.
Looks like I'll have to move on to the Pro ASP.NET book to get what I really want in terms of examples.