Currently, i created a datagrid that enabled user to insert , update and delete from the datagrid..
My problem is..How to validate the input when the user is inserting values into a new row in the datagrid.
Any idea?
Currently, i created a datagrid that enabled user to insert , update and delete from the datagrid..
My problem is..How to validate the input when the user is inserting values into a new row in the datagrid.
Any idea?
Brief summary and practical pattern for ASP.NET (WebForms) DataGrid inserts — builds on the replies from , , and but clarifies what actually applies to WebForms and gives a working approach.
Note on events: the cell-level events mentioned in the thread are typically from Windows Forms grids and do not exist on ASP.NET WebForms DataGrid. For WebForms, perform validation in the server handler that processes the insert (for example the ItemCommand handler that responds to an Insert/Add button in a FooterTemplate or the button click inside your insert template). Client-side checks are fine for user experience but cannot replace server validation.
Pattern to follow (server-side first, client-side for UX):
Example VB.NET snippet (validate in ItemCommand before inserting):
Protected Sub MyDataGrid_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles MyDataGrid.ItemCommand
If e.CommandName <> "Add" Then Return
Dim txtName As TextBox = CType(e.Item.FindControl("txtNameFooter"), TextBox)
Dim txtQty As TextBox = CType(e.Item.FindControl("txtQtyFooter"), TextBox)
Dim qty As Integer
If String.IsNullOrWhiteSpace(txtName.Text) Then
lblError.Text = "Name is required."
Return
End If
If Not Integer.TryParse(txtQty.Text, qty) Then
lblError.Text = "Quantity must be a whole number."
Return
End If
' All validations passed — proceed to insert using parameterized command
End Sub Extra tips
Jump to Post— Ravalon 62Currently, i created a datagrid that enabled user to insert , update and delete from the datagrid..
My problem is..How to validate the input when the user is inserting values into a new row in the datagrid.
Any idea?
With the CellValidating and CellValidated events. :)
maybe you can use the keypress function
Currently, i created a datagrid that enabled user to insert , update and delete from the datagrid..
My problem is..How to validate the input when the user is inserting values into a new row in the datagrid.
Any idea?
With the CellValidating and CellValidated events. :)
just check the value of the control that is used for every function
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.