WebMethod not getting called if binding two GridView using two jQuery AJAX function

Last Reply 8 days ago By dharmendr

Posted 8 days ago

but it's not calling even single webmethod

if there are 2 ajax method to bind 2 gridview then its not calling webmethod

            DataTable dummy = new DataTable();
            dummy.Columns.Add("message");
            dummy.Columns.Add("date");
            dummy.Columns.Add("time");
            dummy.Columns.Add("direction");
            dummy.Rows.Add();
            grdchat.DataSource = dummy;
            grdchat.DataBind();

            DataTable dummy1 = new DataTable();
            dummy1.Columns.Add("message");
            dummy1.Columns.Add("date");
            dummy1.Columns.Add("time");
            dummy1.Columns.Add("direction");
            dummy1.Rows.Add();
            grdchat1.DataSource = dummy1;
            grdchat1.DataBind();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "clientchat();", true);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "consultantchat();", true);

 

<script type="text/javascript">
    function clientchat() {
        $(function () {
            var m = document.getElementById("<%=txtmanagerid.ClientID%>").value;
            var n = document.getElementById("<%=txtclientid.ClientID%>").value;
            $.ajax({
                type: "POST",
                url: "chat.aspx/GetChatDatas",
                data: '{managerid: "' + m + '",clientid:"' + n + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess
            });
            return false;
        });
        function OnSuccess(response) {
            var chatDatas = response.d;
            var row = $("[id*=grdchat] tr:last-child").clone(true);
            $("[id*=grdchat] tr").not($("[id*=grdchat] tr:first-child")).remove();
            for (var i = 1; i < chatDatas.length; i++) {
                for (var j = 0; j < chatDatas[i].length; j++) {
                    $("td", row).eq(j).html(chatDatas[i][j] );
                }
                $("[id *= grdchat]").find("table th").eq(chatDatas[i].length - 1).hide();
                $("[id*=grdchat]").append(row);
                row = $("[id*=grdchat] tr:last-child").clone(true);
            }
        }
    }
</script>
<script type="text/javascript">
    function consultantchat() {
        $(function () {
            var r = document.getElementById("<%=txtmanagerid.ClientID%>").value;
            var s = document.getElementById("<%=txtconsultantid.ClientID%>").value;
            $.ajax({
                type: "POST",
                url: "chat.aspx/GetChatDatas1",
                data: '{managerid: "' + r + '",consultantid:"' + s + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess
            });
            return false;
        });
        function OnSuccess(response1) {
            var chatDatas1 = response1.d;
            var row = $("[id*=grdchat1] tr:last-child").clone(true);
            $("[id*=grdchat1] tr").not($("[id*=grdchat1] tr:first-child")).remove();
            for (var i = 1; i < chatDatas1.length; i++) {
                for (var j = 0; j < chatDatas1[i].length; j++) {$("td", row).eq(j).html(chatDatas[i][j]);
                }
                $("[id *= grdchat1]").find("table th").eq(chatDatas1[i].length - 1).hide();
                $("[id*=grdchat1]").append(row);
                row = $("[id*=grdchat1] tr:last-child").clone(true);
            }
        }
    }
</script>

Webmethod

    [WebMethod]
    public static List<object> GetChatDatas(string managerid,string clientid)
    {
        sqlwebmethod sql = new sqlwebmethod();
        DataSet ds6 = sql.getds("select chatprocess.chat_id,chatprocess.message,chatprocess.sender,direction,convert(varchar(15),date,113) as date,time from chatprocess join managers on managers.managerid=chatprocess.manager_id where chatprocess.manager_id='" + managerid + "' and client_id='" + clientid + "' order by chat_id desc ");

        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] {
        new DataColumn("message"),
        new DataColumn("date"),
        new DataColumn("time"),
        new DataColumn("direction")});
        if (ds6.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds6.Tables[0].Rows.Count; i++)
            {
                dt.Rows.Add(ds6.Tables[0].Rows[i]["message"].ToString(), ds6.Tables[0].Rows[i]["date"].ToString(),ds6.Tables[0].Rows[i]["time"].ToString(), ds6.Tables[0].Rows[i]["direction"].ToString());
            }
        }

        DataSet ds5 = new DataSet();
        ds5.Tables.Add(dt);

        List<object> chatDatas = new List<object>();

        object[] chatData = new object[ds5.Tables[0].Columns.Count];
        for (int i = 0; i < ds5.Tables[0].Columns.Count; i++)
        {
            chatData[i] = ds5.Tables[0].Columns[i].ColumnName;
        }
        chatDatas.Add(chatData);
        for (int i = 0; i < ds5.Tables[0].Rows.Count; i++)
        {
            chatData = new object[ds5.Tables[0].Columns.Count];
            for (int j = 0; j < ds5.Tables[0].Columns.Count; j++)
            {
                chatData[j] = ds5.Tables[0].Rows[i][j];
            }
            chatDatas.Add(chatData);
        }

        return chatDatas;
    }

    [WebMethod]
    public static List<object> GetChatDatas1(string managerid, string consultantid)
    {
        sqlwebmethod sql = new sqlwebmethod();
        DataSet ds6 = sql.getds("select chatprocess.chat_id,chatprocess.message,chatprocess.sender,direction,convert(varchar(15),date,113) as date,time from chatprocess join managers on managers.managerid=chatprocess.manager_id where chatprocess.manager_id='" + managerid + "' and consultant_id='" + consultantid + "' order by chat_id desc ");

        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] {
        new DataColumn("message"),
        new DataColumn("date"),
        new DataColumn("time"),
        new DataColumn("direction")});
        if (ds6.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds6.Tables[0].Rows.Count; i++)
            {
                dt.Rows.Add(ds6.Tables[0].Rows[i]["message"].ToString(), ds6.Tables[0].Rows[i]["date"].ToString(), ds6.Tables[0].Rows[i]["time"].ToString(), ds6.Tables[0].Rows[i]["direction"].ToString());
            }
        }

        DataSet ds5 = new DataSet();
        ds5.Tables.Add(dt);

        List<object> chatDatas = new List<object>();

        object[] chatData = new object[ds5.Tables[0].Columns.Count];
        for (int i = 0; i < ds5.Tables[0].Columns.Count; i++)
        {
            chatData[i] = ds5.Tables[0].Columns[i].ColumnName;
        }
        chatDatas.Add(chatData);
        for (int i = 0; i < ds5.Tables[0].Rows.Count; i++)
        {
            chatData = new object[ds5.Tables[0].Columns.Count];
            for (int j = 0; j < ds5.Tables[0].Columns.Count; j++)
            {
                chatData[j] = ds5.Tables[0].Rows[i][j];
            }
            chatDatas.Add(chatData);
        }

        return chatDatas;
    }

it's not calling webmethod

Posted 8 days ago

Dear @mukesh1,

You have to call two functions in single scriptmanager, if you call two scriptmanagers one by one it executes one scriptmanager only..' 

 

ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "clientchat();consultantchat();", true);

 


Posted 8 days ago Modified on 8 days ago

As per your code you need to modify the naming convention in your code.

  1. You need to give proper name to your GridView instead of grdchart and grdchart1.
  2. You need to provide different function name for different Success method.
  3. Each startup script must have unique name.
  4. Give proper WebMethod name.

Check the modified code.

<div>
    Manager:<asp:TextBox runat="server" ID="txtmanagerid" Text="1" />
    <br />
    Client:<asp:TextBox runat="server" ID="txtclientid" Text="2" />
    <br />
    Consultant:<asp:TextBox runat="server" ID="txtconsultantid" Text="3" />
    <br />
    <br />
    <asp:GridView runat="server" ID="grdClientChat" AutoGenerateColumns="false" Caption="<b>Client Grid</b>">
        <Columns>
            <asp:BoundField DataField="message" HeaderText="message" />
            <asp:BoundField DataField="date" HeaderText="date" />
            <asp:BoundField DataField="time" HeaderText="time" />
            <asp:BoundField DataField="direction" HeaderText="direction" />
        </Columns>
    </asp:GridView>
    <br />
    <asp:GridView runat="server" ID="grdConsultantChat" AutoGenerateColumns="false" Caption="<b>Client Consultant</b>">
        <Columns>
            <asp:BoundField DataField="message" HeaderText="message" />
            <asp:BoundField DataField="date" HeaderText="date" />
            <asp:BoundField DataField="time" HeaderText="time" />
            <asp:BoundField DataField="direction" HeaderText="direction" />
        </Columns>
    </asp:GridView>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    function clientchat() {
        $(function () {
            var m = document.getElementById("<%=txtmanagerid.ClientID%>").value;
            var n = document.getElementById("<%=txtclientid.ClientID%>").value;
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetChatDatas",
                data: '{managerid: "' + m + '",clientid:"' + n + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccessClient,
                error: function (r) { alert(r.responseText); }
            });
            return false;
        });
        function OnSuccessClient(response) {
            var chatDatas = response.d;
            var row = $("[id*=grdClientChat] tr:last-child").clone(true);
            $("[id*=grdClientChat] tr").not($("[id*=grdClientChat] tr:first-child")).remove();
            for (var i = 1; i < chatDatas.length; i++) {
                for (var j = 0; j < chatDatas[i].length; j++) {
                    $("td", row).eq(j).html(chatDatas[i][j]);
                }
                $("[id*=grdClientChat]").find("table th").eq(chatDatas[i].length - 1).hide();
                $("[id*=grdClientChat]").append(row);
                row = $("[id*=grdClientChat] tr:last-child").clone(true);
            }
        }
    }

    function consultantchat() {
        $(function () {
            var r = document.getElementById("<%=txtmanagerid.ClientID%>").value;
            var s = document.getElementById("<%=txtconsultantid.ClientID%>").value;
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetChatDatas1",
                data: '{managerid: "' + r + '",consultantid:"' + s + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccessConsultant,
                error: function (r) { alert(r.responseText); }
            });
            return false;
        });
        function OnSuccessConsultant(response1) {
            var chatDatas1 = response1.d;
            var row = $("[id*=grdConsultantChat] tr:last-child").clone(true);
            $("[id*=grdConsultantChat] tr").not($("[id*=grdConsultantChat] tr:first-child")).remove();
            for (var i = 1; i < chatDatas1.length; i++) {
                for (var j = 0; j < chatDatas1[i].length; j++) {
                    $("td", row).eq(j).html(chatDatas1[i][j]);
                }
                $("[id*=grdConsultantChat]").find("table th").eq(chatDatas1[i].length - 1).hide();
                $("[id*=grdConsultantChat]").append(row);
                row = $("[id*=grdConsultantChat] tr:last-child").clone(true);
            }
        }
    }
</script>

C#

protected void Page_Load(object sender, EventArgs e)
{
    System.Data.DataTable dummy = new System.Data.DataTable();
    dummy.Columns.Add("message");
    dummy.Columns.Add("date");
    dummy.Columns.Add("time");
    dummy.Columns.Add("direction");
    dummy.Rows.Add();
    grdClientChat.DataSource = dummy;
    grdClientChat.DataBind();

    System.Data.DataTable dummy1 = new System.Data.DataTable();
    dummy1.Columns.Add("message");
    dummy1.Columns.Add("date");
    dummy1.Columns.Add("time");
    dummy1.Columns.Add("direction");
    dummy1.Rows.Add();
    grdConsultantChat.DataSource = dummy1;
    grdConsultantChat.DataBind();
    ScriptManager.RegisterStartupScript(this, this.GetType(), "PopClient", "clientchat();", true);
    ScriptManager.RegisterStartupScript(this, this.GetType(), "PopConsultant", "consultantchat();", true);
}

[System.Web.Services.WebMethod]
public static List<object> GetChatDatas(string managerid, string clientid)
{
    List<object> chatDatas = new List<object>();
    chatDatas.Add(new object[] { "message", "date", "time", "direction" });
    chatDatas.Add(new object[] { "message 1", "date 1", "time 1", "direction 1" });
    return chatDatas;
}

[System.Web.Services.WebMethod]
public static List<object> GetChatDatas1(string managerid, string consultantid)
{
    List<object> chatDatas = new List<object>();
    chatDatas.Add(new object[] { "message", "date", "time", "direction" });
    chatDatas.Add(new object[] { "message 2", "date 2", "time 2", "direction 2" });
    return chatDatas;
}

Screenshot

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html