Add Values From Textboxes To GridView Using Jquery
Add Values From Textboxes To GridView Using Jquery
home
asp.net
jquery
excel (vba)
angularjs
javascript
html5
ajax
gridview
demos
http://www.encodedna.com/gridview/tutorial/add-values-from-textbox-to-gridview-using-jquery.htm 1/8
11/6/2017 Add Values from Textboxes to GridView using jQuery
Recent Posts
Excel VBA – Read Data from a Closed Excel File or Workbook without Opening it
Dynamically Add / Remove HTML Elements using jQuery append(), after() and remove() methods
Send Email from an Excel File using VBA Macro and Outlook
We know how to extract data from a data source and bind it with a GridView control using SqlDataSource. In-fact there
is numerous other ways to extract and bind data in Asp.Net using code-behind procedures. However, in this article we
will see how to add values from textboxes to a GridView control using jQuery.
This article is for beginners, who wish to learn some basic technique on data exchange between few textboxes and the
mighty GridView control.
The technique is very simple yet useful. Simply imagine you have few textbox controls and you would like to get the
http://www.encodedna.com/gridview/tutorial/add-values-from-textbox-to-gridview-using-jquery.htm 2/8
11/6/2017 Add Values from Textboxes to GridView using jQuery
values from these textboxes and fill in the columns and simultaneously add new rows with data to the GridView.
The above image explains it all. Let us now see how our example works. I'll first add a GridView control with three
textboxes on my web page.
The Markup
<!DOCTYPE html>
<html>
<head>
<title>Textbox to GridView using jQuery</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<style>
div {
font:11px Verdana;
width:500px;
}
.grid {
width:100%;
font:inherit;
background-color:#FFF;
border:solid 1px #525252;
}
.grid td {
font:inherit;
padding:2px;
border:solid 1px #C1C1C1;
color:#333;
text-align:center;
text-transform:uppercase;
}
.grid th {
padding:3px;
color:#FFF;
background:#424242 url(grd.png) repeat-x top;
border-left:solid 1px #525252;
http://www.encodedna.com/gridview/tutorial/add-values-from-textbox-to-gridview-using-jquery.htm 3/8
11/6/2017 Add Values from Textboxes to GridView using jQuery
font:inherit;
text-align:center;
text-transform:uppercase;
}
ul {
padding:0;
margin:2px 5px;
list-style:none;
border:0;
float:left;
width:70%;
}
li {
width:50%;
float:left;
display:inline-block;
}
</style>
</head>
<body>
<form runat="server">
<div>
<%--MY GRIDVIEW CONTROL--%>
<asp:GridView ID="grdBooks"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="EmpID"
DataSourceID="SqlDataSource1"
CssClass="grid"
GridLines="None">
<Columns>
<asp:BoundField DataField="EmpID" HeaderText="EmpID"
ReadOnly="True" SortExpression="EmpID" />
<asp:BoundField DataField="EmpName" HeaderText="EmpName"
SortExpression="EmpName" />
<asp:BoundField DataField="Designation" HeaderText="Designation"
SortExpression="Designation" />
<asp:BoundField DataField="Department" HeaderText="Department"
SortExpression="Department" />
</Columns>
</asp:GridView>
Inside the <body> section, I have added a GridView control, followed by three labels and textboxes. Using
SqlDataSource, I have populated the Grid with few rows of data. This is easiest way to initialize a GridView control and
add few columns and rows to it.
The Script
<script>
$('#btAdd').click(function () {
var rowCount;
rowCount = $('#grdEmployee tr').length; // GET ROW COUNT.
The above script is executed on button click event. The script has three important sections.
In the first part, I'll get the total row count of the GridView. It provides me the last unique ID of the employee, listed in
the Grid. Later, when necessary, I can save the data in a database table using the unique ID. To get the row count I have
http://www.encodedna.com/gridview/tutorial/add-values-from-textbox-to-gridview-using-jquery.htm 5/8
11/6/2017 Add Values from Textboxes to GridView using jQuery
Second, I'll extract the values from each textbox and add the values to the GridView. In addition, it creates a new row and
adds it to the Grid. Remember, every time you click the button, it will add a new row.
See, how I have used the <table> element features in my jQuery script. This is because, the GridView produces a
<table> like structure when rendered on the browser. Check this article to understand this better.
Finally, I'll clear all the textboxes at once using a jQuery procedure.
$('#divContainer').find('input:text').each(function () {
$('input:text[id=' + $(this).attr('id') + ']').val('');
}
);
All the textboxes are inside a <div> element (a container). Using jQuery .find() and .each() method, I can quickly clear
the input boxes, once values are added to the Grid.
Conclusion
Simple and yet useful, this article just showed you how to extract values from textboxes and add it to a GridView control.
We also saw how to get the total rows count of a GridView control and how we can clear all the input fields with the
click of a single button.
Previous - Count Asp.Net GridView Rows using jQuery Next - Filter or Search Records in a GridView Based on a
length Property Date Range using Asp.Net FilterExpression Property
Like this Article? Subscribe now, and get all the latest articles and tips, right in your inbox.
Related Posts:
http://www.encodedna.com/gridview/tutorial/add-values-from-textbox-to-gridview-using-jquery.htm 6/8
11/6/2017 Add Values from Textboxes to GridView using jQuery
Insert New Records in How to Copy data from Search Records or Filter Dynamically Add
Database Table Using Multiple Sheets to a rows in a GridView Textbox and Button
GridView - C# and... Single Sheet in Excel... Control using Asp.Net using jQuery and Save...
Add Rows to a Table How to Open a New AutoComplete Textbox Show the Sum of
Dynamically using Window from GridView using jQuery and GridView Column Values
jQuery in Asp.Net Using... Asp.Net Web Service -... in Footer in Asp.Net –...
About Me
I am Arun Banik . I live in Mumbai (formerly Bombay), the financial capital of India.
EncodeDna.com is an effort to present good, useful articles and codes for programmers, web developers, database designers and beginners.
The articles and codes are kept simple and to the point.
Connect With Me
Facebook Google+
Twitter RSS
Categories
Ajax AngularJS
Asp.Net AutoComplete
Bootstrap Browser
Charts Crystal Report
http://www.encodedna.com/gridview/tutorial/add-values-from-textbox-to-gridview-using-jquery.htm 7/8
11/6/2017 Add Values from Textboxes to GridView using jQuery
CSS DatePicker
Demos Desktop
Google Chart Google Maps
Google Tutorials GridView
HTML5 JavaScript
Rs.1,499 Rs.699
Jquery JSON
Linq Menus
MS-Excel Plug-in
Responsive Reviews
Send Email Smartphone
Rs.999 Rs.1,289
SQL Server SqlBulkCopy
SqlDataSource VBA
WCF Web API
Web Service XML
Online Tools
Rs.249 Rs.429
Image Resizer
Percentage (%) Calculator
Hex to RGB Converter
Rs.111 Rs.399
http://www.encodedna.com/gridview/tutorial/add-values-from-textbox-to-gridview-using-jquery.htm 8/8