|
Hi friends,
Now I want to bind gridview from webservice using jquery,ajax or javascript..
How can i achive that..
|
|
|
|
|
The solution to your problem comes in three steps
1. Write [WebMethod] (service) to return data from the datatable. But before returning it to your ajax method call convert it to an array. This is because javascript does not understand things such as datatable,dataset,datarow etc...
2. In your aspx page header write an ajax call which should look something like this:-
$(document).ready(function() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/BindDatatable",
data: "{}",
dataType: "json",
success: function(data) {
for (var i = 0; i < data.d.length; i++) {
$("#gvDetails").append("<tr><td>" + data.d[i].UserId + "</td><td>" + data.d[i].UserName + "</td><td>" + data.d[i].Location + "</td></tr>");
}
},
error: function(result) {
alert("Error");
}
});
});
inside the script tags. UserName,UserId,Location are column names from the database.
3. Bind a dummy datatable to your gridview. It should look something like this:
private void BindColumnToGridview()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId");
dt.Columns.Add("UserName");
dt.Columns.Add("Location");
dt.Rows.Add();
gvDetails.DataSource = dt;
gvDetails.DataBind();
gvDetails.Rows[0].Visible = false;
}
this is done so that correct data is mapped to the correct columns in gridview.
All the best.
|
|
|
|