1 Replies - 2637 Views - Last Post: 05 May 2014 - 01:39 PM Rate Topic: -----

#1 bijo62  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-April 14

Gridview does not update after Ajax call

Posted 05 April 2014 - 07:33 PM

Have a Gridview in a page that uses a master page.I Am trying to update the grid after an Ajax call but it does not seem to update.Tried to place it in an update panel but no success.The grid updates fine if I remove the master page from the child page.Any advice would be appreciated.

The ASPX page:

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeBehind="WebForm2.aspx.cs" Inherits="CB.WebForm2" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

<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: "WebForm2.aspx/BindDatatable",
            data: "{}",
            dataType: "json",
            success: function (data) {
                for (var i = 0; i < data.d.length; i++) {                    
                    $("#gvDetails").append("<tr><td>" + data.d[i].FirstName + "</td><td>" + data.d[i].MiddleName + "</td><td>" + data.d[i].LastName + "</td></tr>");
                }
                __doPostBack('<%=UpdatePanel1.ClientID%>', '');
            },
            error: function (result) {
                alert("Error");
            }
        });
    });
</script>
<style type="text/css">
table,th,td
{
border:1px solid black;
border-collapse:collapse;
}
</style>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
<asp:GridView ID="gvDetails" runat="server">
<HeaderStyle BackColor="#DC5807" Font-Bold="true" ForeColor="White" />
</asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Content>

Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Xml;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

namespace CB
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindColumnToGridview();
            }
        }
        private void BindColumnToGridview()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FirstName");
            dt.Columns.Add("MiddleName");
            dt.Columns.Add("LastName");
            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(ConfigurationManager.ConnectionStrings["CampaignConnection"].ToString()))
            {
                try
                {
                    using (SqlCommand cmd = new SqlCommand("select * from Candidates", con))
                    {
                        con.Open();
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        da.Fill(dt);
                        foreach (DataRow dtrow in dt.Rows)
                        {
                            UserDetails user = new UserDetails();
                            user.FirstName = dtrow["FirstName"].ToString();
                            user.MiddleName = dtrow["MiddleName"].ToString();
                            user.LastName = dtrow["LastName"].ToString();
                            details.Add(user);
                        }                                      
                    }
                }                                   
                catch (Exception ex)
                {
                    var aa = ex.Message;
                }
            }
            return details.ToArray();
        }
        public class UserDetails
        {
            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            public string LastName { get; set; }
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Gridview does not update after Ajax call

#2 trevster344  Icon User is online

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Gridview does not update after Ajax call

Posted 05 May 2014 - 01:39 PM

Is the request actually being made, and if so is it actually returning the data you are looking for?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1