How to Validate GridView Fields in ASP.Net

Introduction

The most common control used in any website or any web application is a GridView. The GridView will subsequently have various controls inside it such as TextBox and drop down lists. You will generally encounter a requirement that each row should be validated independently of other rows.

Scenario

You have a GridView that displays data of an employee and has a TextBox that accepts an employee address. Please find the screenshot for reference.

gridview Validation

If you simply add a button and a required field validator to validate against the address TextBox, a single button will validate against all the rows.

required field validator

The logic used to perform the validation will assign a unique validation group for each row. Now to decide where to set this group validation is the event where the binding of the grid happens. The binding of the grid happens on the RowDataBound Event, that is why we assign a validation group to the controls present in each row inside in the RowDataBound event.

RowDataBound event

The implementation logic is explained below using a workflow below:

Implementation logic

Step 1: An Employee class is created that will, in turn, be used for populating the GridView:
  1. public class Employee  
  2. {  
  3.     string id;  
  4.     public string Id  
  5.     {  
  6.         get { return id; }  
  7.         set { id = value; }  
  8.     }  
  9.     string empName;  
  10.     public string EmpName  
  11.     {  
  12.         get { return empName; }  
  13.         set { empName = value; }  
  14.     }  
  15.     string deptName;  
  16.     public string DeptName  
  17.     {  
  18.         get { return deptName; }  
  19.         set { deptName = value; }  
  20.     }  
  21.     string address;  
  22.     public string Address  
  23.     {  
  24.         get { return address; }  
  25.         set { address = value; }  
  26.     }  
  27.     public Employee(string id,string name,string dept)  
  28.     {  
  29.         Id = id;  
  30.         EmpName = name;  
  31.         DeptName = dept;  
  32.     }  
  33. } 

Step 2: Declare a variable for storing ValidationGroup as in the following:

  1. int groupName;  

 

Step 3: On the Page_Load event write the following code:

  1. if (!IsPostBack)  
  2. {  
  3.     Employee empObj1 = new Employee("101""Ravi ""ENR");  
  4.     Employee empObj2 = new Employee("102""Sanoj ""RCL");  
  5.     Employee empObj3 = new Employee("103""Abhishek""FSI");  
  6.     List<Employee> lstEmp = new List<Employee>();  
  7.     lstEmp.Add(empObj1);  
  8.     lstEmp.Add(empObj2);  
  9.     lstEmp.Add(empObj3);  
  10.     gvEmployee.DataSource = lstEmp;  
  11.     gvEmployee.DataBind();  
  12. }  
  13. groupName = 0;

Step 4: On the Gridview's RowDataBound event write the following code:

  1. if (e.Row.RowType == DataControlRowType.DataRow)  
  2. {  
  3.     RequiredFieldValidator objAddress = e.Row.FindControl("rfvAddress"as RequiredFieldValidator;  
  4.     objAddress.ValidationGroup = groupName.ToString();  
  5.     Button objAdd = e.Row.FindControl("btnAdd"as Button;  
  6.     objAdd.ValidationGroup = groupName.ToString();  
  7.     TextBox objtxtAddress = e.Row.FindControl("txtAddress"as TextBox;  
  8.     objtxtAddress.ValidationGroup = groupName.ToString();  
  9.     groupName++;  
  10. } 

Reference(s)

msdn.microsoft


Similar Articles