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