Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Bind/BindDatatable",---->check here plz 
            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");
            }
        });
    });
</script>
<asp:GridView ID="gvDetails" runat="server">
<HeaderStyle BackColor="#DC5807" Font-Bold="true" ForeColor="White" />

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindColumnToGridview();
            }
        }
        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;
        }

        [WebMethod]
        public static UserDetails[] BindDatatable()
        {
            DataTable dt = new DataTable();
            List<UserDetails> details = new List<UserDetails>();

            using (SqlConnection con = new SqlConnection("Data Source=184.168.47.17;Initial Catalog=Question_Papers;User ID=Shravan;Password=Shravan@34; Trusted_Connection=False"))
            {
                using (SqlCommand cmd = new SqlCommand("select TOP 10 UserId,UserName,Location from Test_1", con))
                {
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    foreach (DataRow dtrow in dt.Rows)
                    {
                        UserDetails user = new UserDetails();
                        user.UserId = dtrow["UserId"].ToString();
                        user.UserName = dtrow["UserName"].ToString();
                        user.Location = dtrow["Location"].ToString();
                        details.Add(user);
                    }
                }
            }
            return details.ToArray();
        }
        public class UserDetails
        {
            public string UserId { get; set; }
            public string UserName { get; set; }
            public string Location { get; set; }
        }
    }
Posted
Comments
F-ES Sitecore 3-Jun-15 11:58am    
If you're not doing server-side data binding then using asp controls are pointless. Just put a div on the table, or even a <table> with no <tr> elements then inject the tr elements via your jQuery.
iamvinod34 3-Jun-15 13:04pm    
ya...but no answer sir...

1 solution

In URL you should give the page name with extension and method name

So the url should look like

JavaScript
url: "Default.aspx/MethodName"
 
Share this answer
 
Comments
iamvinod34 4-Jun-15 6:04am    
this is web form page sir...
url: "Default.aspx/MethodName"---> this syntax not executed sir
url: "Default/MethodName" ----> this syntax else part did executed sir
so plz help me sir...
3-days i tried this prlbm
[no name] 5-Jun-15 4:09am    
You mentioned that your datatype is json. so you have to return a json object from the server side instead of returning the array. I think that change should solve your problem
iamvinod34 6-Jun-15 3:16am    
plz tell me one example plz
help me plz
[no name] 6-Jun-15 12:39pm    
use newton.json to serialize the object to json object and use jquery '$each' to iterate through list of object in sucess fucntion

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