In this article I will explain with an example, how to validate TextBox inside GridView using JavaScript on Button Click in ASP.Net.
When the Submit Button is clicked, using a FOR loop, all the TextBoxes inside the GridView will be validated and respective Error messages will be displayed using JavaScript in ASP.Net.
HTML Markup
The HTML Markup consists of an ASP.Net GridView with one BoundField column and two TemplateField columns consisting of TextBoxes.
Below the GridView, there is a Button which has been assigned an OnClientClick event handler.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <asp:BoundField DataField="Item" HeaderText="Item" />
        <asp:TemplateField HeaderText="Price">
                <asp:TextBox ID="txtPrice" runat="server" Text='<%# Eval("Price") %>'></asp:TextBox><br />
                <span class="error">Required</span>
        <asp:TemplateField HeaderText="Quantity">
                <asp:TextBox ID="txtQuantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:TextBox><br />
                <span class="error">Required</span>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return Validate()" />
You will need to import the following namespace.
using System.Data;
Imports System.Data
Binding the GridView control
Inside the Page Load event handler, the GridView is populated with a dynamic DataTable with some dummy data.
Note: You can learn more about this technique in my article Create DataTable dynamically and bind to GridView in ASP.Net.
protected void Page_Load(object sender, EventArgs e)
    if (!this.IsPostBack)
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Item"), new DataColumn("Price"), new DataColumn("Quantity") });
        dt.Rows.Add("Football", 299, 10);
        dt.Rows.Add("Shirt", 545, 15);
        dt.Rows.Add("Disc", 99, 20);
        dt.Rows.Add("Watch", 200, 45);
        dt.Rows.Add("Clock", 670, 97);
        GridView1.DataSource = dt;
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable
        dt.Columns.AddRange(New DataColumn() {New DataColumn("Item"), New DataColumn("Price"), New DataColumn("Quantity")})
        dt.Rows.Add("Football", 299, 10)
        dt.Rows.Add("Shirt", 545, 15)
        dt.Rows.Add("Disc", 99, 20)
        dt.Rows.Add("Watch", 200, 45)
        dt.Rows.Add("Clock", 670, 97)
        GridView1.DataSource = dt
    End If
End Sub
Validating TextBox inside GridView using JavaScript
When the Submit Button is clicked, the following JavaScript function is called.
Inside this function, the TextBoxes inside the GridView are referenced and using a loop, each TextBox is validated and if the validation fails, the respective Error Label is displayed.
<script type="text/javascript">
    function Validate() {
        //Reference the GridView.
        var grid = document.getElementById("<%=GridView1.ClientID %>");
        //Reference all INPUT elements.
        var inputs = grid.getElementsByTagName("INPUT");
        //Set the Validation Flag to True.
        var isValid = true;
        for (var i = 0; i < inputs.length; i++) {
            //If TextBox.
            if (inputs[i].type == "text") {
                //Reference the Error Label.
                var label = inputs[i].parentNode.getElementsByTagName("SPAN")[0];
                //If Blank, display Error Label.
                if (inputs[i].value == "") {
           = "block";
                    isValid = false;
                } else {
           = "none";
        return isValid;
The following CSS style is used for the Error Label.
<style type="text/css">
        color: Red;
        display: none;
Validate TextBox inside GridView using JavaScript on Button Click in ASP.Net
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.