Return ArrayList to AJAX response and add embed object to play sound file in ASP.Net

Last Reply 6 months ago By dharmendr

Posted 6 months ago

i am using webuser control on which i m calling webmethod on javascript timer after every 3 seconds.i want to show toast msg with help of this webmethod.on master page i m using this web method.

<script type="text/javascript">
    function myFunction() {
        // alert("Hello");
      
        $.ajax({
            type: "POST",
            url: "text.aspx/getnotifications",
            data: '',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (r) {  }
        });
       
        function OnSuccess(response) {
       $.each(response.d, function () {
            this['Value'];
       })
        }
    }
</script>

in this i need to play sound and show toast msg.

Webmethod code....

    [WebMethod]
    public static ArrayList getnotifications()
    {
        ArrayList list = new ArrayList();
        DataSet ds = sql.getds("select candidates.name,clients.client_id from interview join candidates on candidates.candidateid=interview.candidateid  join clients on clients.client_id=interview.clientid where interview.date='" + DateTime.Now.ToString() + "' and interview.attendstatus='" + "Interview" + "'");

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataSet ds6 = sql.getds("select clientid from clientallot where managerid='" + HttpContext.Current.Session["idmanager"].ToString() + "'");
                    for (int j = 0; j < ds6.Tables[0].Rows.Count; j++)
                    {
                        if (ds.Tables[0].Rows[i]["client_id"].ToString() == ds6.Tables[0].Rows[j]["clientid"].ToString())
                        {
                            string text1 = "Candidate " + ds.Tables[0].Rows[i]["name"].ToString() + " interview for role " + ds.Tables[0].Rows[i]["role"].ToString() + " with company "
                          + ds.Tables[0].Rows[i]["company"].ToString() + " is not confirmed ";
						  
                           // ScriptManager.RegisterStartupScript(Page, this.GetType(), "Alert" + i, "<script>showpop('" + text1 + "')</script>", false);
                           list.Add(new ListItem(showpop('" + text1 + "'));
                        }
                    }
                }
				
		DataSet ds10 = sql.getds("select distinct client_id from chatprocess where manager_id='" + HttpContext.Current.Session["idmanager"].ToString() + "' and client_id!='" + "" + "' and sender='" + "Client" + "' and seen='" + "no" + "'");
                if (ds10.Tables[0].Rows.Count > 0)
                {
                    for (int m = 0; m < ds10.Tables[0].Rows.Count; m++)
                    {
                        string clientname = sql.getsinglevallue("select company from clients where client_id='" + ds10.Tables[0].Rows[m]["client_id"].ToString() + "'");
                        string text6 = "You have received message from " + clientname + "";
                        //ScriptManager.RegisterStartupScript(Page, this.GetType(), "Alert" + m, "<script>showpop5('" + text6 + "')</script>", false);
                        list.Add(new ListItem(showpop('" + text1 + "'));
                    }
                }
            return list;
        }				

How to find array list value in javascript i want to find value like showpop("mesggae here") showpop("ur done") showpop5("u received msg from admin")

i have code in master page javascript for javascript function showpop.that will work if i m able to show msg like this.

also need to emed code for sound like 

"<embed src='sms-alert-5-daniel_simon.wav' id='beep' width='0' height='0' enablejavascript='true'>"

i don't understand how to run this on javascript

You are viewing reply posted by: dharmendr 6 months ago.
Posted 6 months ago

Hi mukesh1,

A WebMethod is called by AJAX. You can write JavaScript function to run after your AJAX call returns, but you can't call javascript inside your C#.

Call the JavaScript function upon successful completion (callback) of the call to the WebMethod in your JS code.

Check this example. Now please take its reference and correct your code.

HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
<link media="screen" rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"></script>
<script type="text/javascript">
    function myFunction() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/getnotifications",
            data: '',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (r) { }
        });

        function OnSuccess(response) {
            $.each(response.d, function () {
                var text = this.Text;
                showpop(text, "Lert");
                var obj = document.createElement("embed");
                obj.setAttribute('src', 'sms-alert-5-daniel_simon.wav');
                obj.setAttribute('id', 'beep');
                obj.setAttribute('width', '0');
                obj.setAttribute('height', '0');
                obj.setAttribute('enablejavascript', 'true');
                document.body.appendChild(obj);
            })
        }
    }
    function showpop(msg) {
        toastr.info(msg);
    }
    $(function () {
        myFunction();
    });
</script>

C#

[System.Web.Services.WebMethod]
public static ArrayList getnotifications()
{
    ArrayList list = new ArrayList();
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("client_id", typeof(int)), new DataColumn("name"), new DataColumn("role"), new DataColumn("company") });
    dt.Rows.Add(1, "John Hammond", "Tester", "Test 1");
    dt.Rows.Add(2, "Mudassar Khan", "MD", "Test 2");
    dt.Rows.Add(3, "Suzanne Mathews", "Developer", "Test 3");
    dt.Rows.Add(4, "Robert Schidner", "Engineer", "Test 4");
    DataSet ds = new DataSet();
    ds.Tables.Add(dt);

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        DataTable dt1 = new DataTable();
        dt1.Columns.AddRange(new DataColumn[] { new DataColumn("clientid", typeof(int)) });
        dt1.Rows.Add(1);
        dt1.Rows.Add(2);
        dt1.Rows.Add(5);
        dt1.Rows.Add(6);
        DataSet ds6 = new DataSet();
        ds6.Tables.Add(dt1);
        for (int j = 0; j < ds6.Tables[0].Rows.Count; j++)
        {
            if (ds.Tables[0].Rows[i]["client_id"].ToString() == ds6.Tables[0].Rows[j]["clientid"].ToString())
            {
                string text1 = "Candidate " + ds.Tables[0].Rows[i]["name"].ToString()
                            + " interview for role " + ds.Tables[0].Rows[i]["role"].ToString()
                            + " with company " + ds.Tables[0].Rows[i]["company"].ToString() + " is not confirmed ";
                list.Add(new ListItem(text1));
            }
        }
    }
    return list;
}

Screenshot