Click here to Skip to main content
15,916,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to bind value to datalist or gridview using javascript or jquery.

I have used the web method and jquery to bind the value to the gridview. For doing this i have used webmethod and my web method will return a list with all the values in it and i have used jquery to call the method and the value is comming successfully but when i bind the value to my asp gridview it is not throwing any error but the value is not binding.

The following is the code. And the value is comming perfectly in the variable details

success:
C#
function (details)
 { $("#<%=GridView1.ClientID %>").append(details.d); }



All the functions are executing successfully but my grid is not showing.
My complete solution is

public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}
[WebMethod]
public static Name[] LoadValues()
{
SqlConnection conn = new SqlConnection(@"Data Source=CLAYSYSCH0028\Sharepoint;Initial Catalog=ShankarMS;Integrated Security=True");
conn.Open();
List<name> list = new List<name>();
SqlCommand comm = new SqlCommand();
comm.CommandText = "select * from UserDetails";
comm.Connection = conn;
SqlDataAdapter adapter = new SqlDataAdapter(comm);
DataSet set = new DataSet();
adapter.Fill(set);
DataTable table = set.Tables[0];
foreach (DataRow row in table.Rows)
{
Name n = new Name();
n.names = row["FirstName"].ToString();
list.Add(n);
}
return list.ToArray();
}
}
public class Name
{
public string names
{
get;
set;
}
}

jquery for binding to grid view

XML
<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: "POST",
                url: "WebForm3.aspx/LoadValues",
                data: "{}",
                contentType: "application/json",
                dataType: "json",
                success: function (data) {

                    for (var i = 0; i < data.d.length; i++) {

                        $("#<%=GridView1.ClientID %>").append("<tr><td>" + data.d[i].names + "</td></tr>");
                    }

                },
                Error: function () {
                    debugger;
                }

            })
        });

    </script>




Asp Grid View

XML
<asp:GridView ID="GridView1" runat="server" ShowHeaderWhenEmpty="True">
        </asp:GridView>
Posted
Updated 10-Apr-17 10:56am
v4

There are many example for grid binding using jquery[^] and also javascript[^].try google once before asking here

and even check this also

Asp:GridView DataBind in Javascript[^]
 
Share this answer
 
Comments
udayams 28-Oct-13 6:50am    
Thanks

After watching your answer i found the mistake i have done.

I have not created the column name for my gridview items.
[no name] 28-Oct-13 6:53am    
Glad it helped..
 
Share this answer
 
Comments
udayams 28-Oct-13 6:50am    
Thanks

After watching your answer i found the mistake i have done.

I have not created the column name for my gridview items.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900