Validating in a DataGrid
Validating the data before it is even submitted saves a round-trip. However, for some database logic you may need to have a round-trip (for CustomValidators).
<asp:templatecolumn> <itemtemplate> <%# Container.DataItem("Age") %> </itemtemplate> <edititemtemplate> <asp:RegularExpressionValidator runat="server" ControlToValidate="txtAge" ErrorMessage="Invalid input for Age" ValidationExpression="[0-9]{2}" /><br> <asp:textbox runat="server" id="txtAge" value='<%# Container.DataItem("Age") %>' /> </edititemtemplate> </asp:templatecolumn> |
Live Demo |
This checks to make sure that it is a two digit number.
Summary
This article has more been about using TemplateColumns than using web forms, however we have used a series of server controls (RadioButton, checkbox and validation) in a DataGrid to give it more functionality. In the related article - "Using Dropdowns in DataGrids" I will explore using a DropDownList which is more complicated than the usual server controls.