Articles → JQUERY → Bind Asp.Net Gridview In Jquery
Bind Asp.Net Gridview In Jquery
Software Requirement
- Visual studio 2008(or above) is installed on your machine.
- SQL server management studio (any version) is installed on your machine.
Prerequisite Knowledge
- How to create a project using visual studio
- How to create tables and insert data into table using SSMS
- Basics of ado.net
- Basics about data controls like gridview, datalist etc
- Basics about jQuery
- How to call jquery's ajax method
Steps Of Execution
- Create a new project
- Create a new table
- Add code to fetch data from database
- Add code to bind columns in gridview
- Add controls on aspx page
- Write jQuery code to bind drop down
Create A New Project
Click to Enlarge
Create A New Table
Click to Enlarge
Add Code To Fetch Data From Database
[WebMethod]
public static string GetNames() {
DataSet ds = new DataSet("student");
using(SqlConnection conn = new SqlConnection(@"conn_string")) {
conn.Open();
using(SqlDataAdapter adap = new SqlDataAdapter("select student_name,age from student", conn)) {
adap.Fill(ds);
}
}
return ds.GetXml();
}
Add Code To Bind Columns In Gridview
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
DataTable dummy = new DataTable();
dummy.Columns.Add("name");
dummy.Columns.Add("age");
dummy.Rows.Add();
dlCustomers.DataSource = dummy;
dlCustomers.DataBind();
}
}
Add Controls On Aspx Page
<asp:GridView ID="gvCustomers" runat="server"> </asp:GridView>
<asp:Button ID="btnBind" runat="server" Text="Bind" />
Write Jquery Code To Bind Drop Down
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type = "text/javascript">
$(document).ready(
function() {
$("#<%=btnBind.ClientID %>").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetNames",
data: "{}",
contentType: "application/json",
dataType: "json",
success: function(response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("student").find("Table");
var str = "";
for (i = 0; i < customers.length; i++) {
str = str + "<tr><td>";
str = str + $(customers[i]).find("student_name").text();
str = str + "</td><td>";
str = str + $(customers[i]).find("age").text();
str = str + "</td></tr>";
}
$("#<%=gvCustomers.ClientID %>").append(str);
}
});
return false;
});
});
</script>
Output
Click to Enlarge
Full Code
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(
function () {
$("#<%=btnBind.ClientID %>").click(function () {
$.ajax({
type: "POST",
url: "Default.aspx/GetNames",
data: "{}",
contentType: "application/json",
dataType: "json",
success: function (response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("student").find("Table");
var str = "";
for (i = 0; i < customers.length; i++) {
str = str + "<tr><td>";
str = str + $(customers[i]).find("student_name").text();
str = str + "</td><td>";
str = str + $(customers[i]).find("age").text();
str = str + "</td></tr>";
}
$("#<%=gvCustomers.ClientID %>").append(str);
}
});
return false;
});
});
</script>
<asp:GridView ID="gvCustomers" runat="server">
</asp:GridView>
<asp:Button ID="btnBind" runat="server" Text="Bind" />
</asp:Content>
using System;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
public partial class _Default: System.Web.UI.Page {
/// <summary>
/// Page load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
DataTable dummy = new DataTable();
dummy.Columns.Add("name");
dummy.Columns.Add("age");
dummy.Rows.Add();
gvCustomers.DataSource = dummy;
gvCustomers.DataBind();
}
}
[WebMethod]
public static string GetNames() {
DataSet ds = new DataSet("student");
using(SqlConnection conn = new SqlConnection(@"conn_string")) {
conn.Open();
using(SqlDataAdapter adap = new SqlDataAdapter("select student_name,age from student", conn)) {
adap.Fill(ds);
}
}
return ds.GetXml();
}
}